1

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

Leo
  • 835
  • 1
  • 12
  • 31

1 Answers1

1

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.

Daniyar
  • 2,975
  • 2
  • 26
  • 39