iOS-Swift UIApplication.shared.isIdleTimerDisabled = true not working after review from AppStore
Solution 1:
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.
Solution 2:
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.