相关文章推荐
开朗的楼梯  ·  toString()、String.valu ...·  1 年前    · 
温柔的西瓜  ·  Android ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

iOS-Swift UIApplication.shared.isIdleTimerDisabled = true not working after review from AppStore

Ask Question

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.

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
                When is your VC loaded? You set the IdleTimerDisabled in viewDidLoad which might only occur once if it is in a Navigation stack and get disabled when you push or pop because of your viewDidDisappear? Do you want to reinstate it in viewDidAppear?
– Matt_S
                Jan 2, 2019 at 12:03
                Well the VC is embedded in a Navigation controller. Even if the code is at viewDidLoad, ideally it should load once. But the problem is it doesn't even load at all. I think it is a bug in iOS
– Hanz Cheah
                Jan 2, 2019 at 12:08
                Is there a solution to this issue? I have a video call app, and I need the device to disable auto-dimming of the screen during the video calls. Is there a way to achieve this
– Miki
                Oct 10, 2019 at 14:31

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.

You don't have to always set the isIdleTimerDisabled to false from the SceneDelegate. In the case of the article you provided, the need was to set the isIdleTimerDisabled to false when the application starts. In that specific case the Application Lifecycle matters, so you might need to set the flag to false from the SceneDelegate. In the above case, though, there is no reason to call isIdleTimerDisabled from the SceneDelegate. You can call it from any place in your application. – Vladimir Jun 30, 2023 at 17:58

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.

Here's how to set isIdleTimerDisabled from the scene delegate for a SwiftUI application on iOS 13+:

import SwiftUI
@main
struct MyApp: App {
    @Environment(\.scenePhase) var scenePhase
    var body: some Scene {
        WindowGroup {
            MainView()
        .onChange(of: scenePhase) { (phase) in
            switch phase {
            case .active:
                // In iOS 13+, idle timer needs to be set in scene to override default
                UIApplication.shared.isIdleTimerDisabled = true
            case .inactive: break
            case .background: break
            @unknown default: print("ScenePhase: unexpected state")

You must be opening another view controller from current view controller. That set the value of isIdleTimerDisabled to false when viewDidDisappear calls. So, you should set UIApplication.shared.isIdleTimerDisabled = true in viewDidAppear. Hope this will solve your issue.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.