Screen turn into dark when set UIApplication.shared.isIdleTimerDisabled to false

1.3k views Asked by At

I want screen always light when playing audio, and turn dark when complete the audio and I do not tap the screen after two minutes(i have set auto-lock in setting), implement code as the following.

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        UIApplication.shared.isIdleTimerDisabled = true
    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        UIApplication.shared.isIdleTimerDisabled = false
    }

But the result is screen turn dark immediately when finish one audio which is more than two minutes.

How to fix it ,or is this ios bug?

Thanks

1

There are 1 answers

0
Daniyar On

Seems like there isn't any opportunity to reset idle timer thus your device goes off immediately after the isIdleTimerDisabled-property set.

Yet you still can try to implement your own timer to turn it off in right time.

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)
    DispatchQueue.main.asyncAfter(deadline: .now() + interval, execute: {
        UIApplication.shared.isIdleTimerDisabled = false
    })
}

Unfortunately, there still isn't any way to check current idleTimer time to calculate interval value dynamically.