简体   繁体   中英

How to use a timer on “idle timer”

I am making an app where I would like to make the app go to sleep in 2 hours after a button is pressed. I have seen some stuff online with using "idle timer" to deactivate it. But I have not seen how you put this on a timer.

Well, turn off device programmatically is not possible. It is possible to put app to sleep if idle timer was used to make it always on.

So, somewhere at start of app:

UIApplication.shared.isIdleTimerDisabled = true;

Now when app is running display will be always on.

Now user hit "Sleep in 2hours" button. Inside button handler code insert this:

_ = Timer.scheduledTimer(withTimeInterval: 3600*2, repeats: false, block: { (Timer) in
        UIApplication.shared.isIdleTimerDisabled = false;
    })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM