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.
Asked
Active
Viewed 948 times
0
-
What do you mean by "make the app go to sleep"? – rmaddy Jan 06 '18 at 04:46
-
turn the whole device off. Not sure it down just put the whole app to sleep. – Jan 06 '18 at 04:48
1 Answers
1
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;
})

Juraj Antas
- 3,059
- 27
- 37