139

How can I prevent screen lock only when using Navigation?

Waze has the option to do that, how can I do this in my App?

pkamb
  • 33,281
  • 23
  • 160
  • 191
alvarogalia
  • 1,601
  • 2
  • 12
  • 10

5 Answers5

284

Use this:

Objective-C:

[[UIApplication sharedApplication] setIdleTimerDisabled: YES];

Swift (legacy):

UIApplication.sharedApplication().idleTimerDisabled = true

Swift 3 and above:

UIApplication.shared.isIdleTimerDisabled = true

Make sure to import UIKit.

Here is the link to the documentation from developer.apple.com.

atwalsh
  • 3,622
  • 1
  • 19
  • 38
25

For Swift 3.0 here are two options depending on where you want to invoke the code:

Inside AppDelegate.swift:

application.idleTimerDisabled = true

Outside AppDelegate.swift:

UIApplication.shared().isIdleTimerDisabled = true

Crashalot
  • 33,605
  • 61
  • 269
  • 439
16

Swift 4

in AppDelegate.swift file, add the following line inside application function:

application.isIdleTimerDisabled = true
        
aturan23
  • 4,798
  • 4
  • 28
  • 52
kavehmb
  • 9,822
  • 1
  • 19
  • 22
6

You can use my little lib Insomnia (Swift 3, iOS 9+) - another nice feature is that you can prevent from sleeping only when charging.

The idleTimerDisabled soultion is okay but you have to remember to set it to false afterwards.

nsmeme
  • 221
  • 4
  • 1
0

If you have more advanced case you can use our small project: ScreenSleepManager or if it's just about particular ViewControllers - use Insomnia as pointed earlier. Manual dealing with idleTimerDisabled almost always caused me some issues (like forgot to re-set to false or handle multiple (nested) modules trying to set it).

Dev_IL
  • 252
  • 1
  • 15