Trying to Achieve
Stop the screen going to sleep on certain View Controller
What I have done
I have tested my app with the following code on a physical iPhone 6 running on iOS 12.1.12. I set the phone Dislpay & Brigthness > Auto-Lock
to 30 seconds. I ran the app and on the particular VC with UIApplication.shared.isIdleTimerDisabled = true
, the screen remains on even after 30 seconds. And when I switch to other VC without UIApplication.shared.isIdleTimerDisabled = true
, the screen will turn off after 30 seconds.
I distributed to AppStore and I am positive that I have uploaded the correct version and dowloaded a fresh copy from AppStore, the screen always on thingy doesn't work at all, the screen will always turn off following the Auto-Lock
setting.
I have read some articles https://docs.kioskproapp.com/article/899-ios-12-with-guided-access-causing-kiosk-pro-to-sleep and isIdleTimerDisabled not working in iOS 12
I tried the Guided Access
and switched on Guided Access
and Mirror Display Auto-Lock > ON
. Still it is not working for the App from AppStore. Please help.
Code
override func viewDidLoad() {
super.viewDidLoad()
//==== For the screen to remains on
UIApplication.shared.isIdleTimerDisabled = true
}
override func viewDidDisappear(_ animated: Bool) {
//=== Switch off the screen always on, back to the phone settings.
UIApplication.shared.isIdleTimerDisabled = false
}
I had the same issue on iOS 13. And I found this great article explaining why .isIdleTimerDiabled
appears to be not working.
In short, as iOS 13 introduced SceneDelegate, just by setting .isIdleTimerDisabled
to false in AppDelegate is not enough, you need to set it in SceneDelegate too
class SceneDelegate: NSObject, UIWindowSceneDelegate {
var window: UIWindow?
@available(iOS 13.0, *)
func sceneDidBecomeActive(_ scene: UIScene) {
UIApplication.shared.isIdleTimerDisabled = true
}
}
Code above made my app immune to autolock, but this is just a quick dirty solution, a better way is to toggle isIdleTimerDisabled
on and off based on ViewController user is using, below is Apple's documentation about this property.
You should set this property (
isIdleTimerDisabled
) only if necessary and should be sure to reset it to false when the need no longer exists. Most apps should let the system turn off the screen when the idle timer elapses. This includes audio apps. With appropriate use of Audio Session Services, playback and recording proceed uninterrupted when the screen turns off. The only apps that should disable the idle timer are mapping apps, games, or programs where the app needs to continue displaying content when user interaction is minimal.
Just wanted to also share that the provided answer from https://stackoverflow.com/a/64235679/14414215 @Daniel Hu was great but I had issues why it wasn't working while running it thru Xcode. This little snippet was the key (from This Link)
Test Note: Running applications under the debugger through Xcode will automatically disable the idle timer. When building or testing the app, make sure to install it, then launch it “by hand” on a test device. The effects will be most obvious if the “Auto-Lock” setting is 30 seconds.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With