164

I have an app that must stay awake until the end of a countdown but it will go into 'sleep mode' whenever it reaches the allocated time to sleep.

In my app, I have the option to postpone sleep, so users can disable/enable it.

How do I do it programmatically?

Wyetro
  • 8,439
  • 9
  • 46
  • 64
Arnlee Vizcayno
  • 2,405
  • 3
  • 23
  • 31

4 Answers4

389

You can disable the idle timer as follows;

In Objective-C:

[UIApplication sharedApplication].idleTimerDisabled = YES;

In Swift:

UIApplication.sharedApplication().idleTimerDisabled = true

In Swift 3.0 & Swift 4.0:

UIApplication.shared.isIdleTimerDisabled = true

Set it back to NO or false to re-enable sleep mode.

For example, if you need it until you leave the view you can set it back by overriding the viewWillDisappear:

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

More about UIApplication Class.

zimmerrol
  • 4,872
  • 3
  • 22
  • 41
jrturton
  • 118,105
  • 32
  • 252
  • 268
  • @Gati I doubt it, but I can't confirm since I don't have a watch – jrturton Jun 18 '15 at 06:59
  • 1
    @jrturton Hmm.. It is not working in apple watch. If you got any solution for this then please let me know. – iGatiTech Jun 18 '15 at 07:16
  • Thanks for this information. It will come in handy. I was wondering if disabling sleep mode in one app will affect the entire device. Like if I disable it in my app and then exit the app, will it still be disabled after the app exits? Do I need to check whether it's active or not, and then reenable it on the way out? – Scott Kilbourn Sep 16 '15 at 01:46
  • 2
    @ScottKilbourn it only affects your app. `[UIApplication sharedApplication]` refers to _your_ app's UIApplication object only. – jrturton Sep 16 '15 at 05:55
24

In Swift 3, to disable the idle timer it is now:

UIApplication.shared.isIdleTimerDisabled = true

To turn the idle timer back on it is simply:

UIApplication.shared.isIdleTimerDisabled = false

Additionally, note that YES and NO are not available in Swift and that you must use either true or false (as opposed to the previous answer).

Wyetro
  • 8,439
  • 9
  • 46
  • 64
4

iOS 13, Swift 5,5.1+ to disable the idle timer. In SceneDelegate.swift.

 func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
     UIApplication.shared.isIdleTimerDisabled = true
 }
Sandeep Maurya
  • 1,979
  • 17
  • 22
-6

in Swift 3 exact location where this can be done is AppDelegate.swift - you should add UIApplication.shared.isIdleTimerDisabled = true inside application func so result will look like this:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    UIApplication.shared.isIdleTimerDisabled = true
    return true
}
godblessstrawberry
  • 4,556
  • 2
  • 40
  • 58
  • 2
    This is not reliant on AppDelegate. In fact I would consider it bad practice to keep the device awake without any real reason. The question was about keeping the device awake during a specific functionality (until the end of a countdown). So disabling the idle timer when the countdown starts end enabling it again after the countdown ends should be considered. – Martin Sep 07 '17 at 06:48
  • @Cyrus I'm not swift programmer. currently I'm making webview app and I tried a lot of solutions without exact instructions and that didnt work for me, but THIS WORKS. so I post this to share with others who will be searching for similar solution. the reason for using this - my app is guitar helper that needs to be always awaking during the playing – godblessstrawberry Sep 07 '17 at 06:59