Arnlee Vizcayno Arnlee Vizcayno - 1 month ago 14
iOS Question

How to disable/enable the sleep mode programmatically in iOS?

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?

Answer

You can disable the idle timer as follows;

In Objective-C:

[UIApplication sharedApplication].idleTimerDisabled = YES;

In Swift:

UIApplication.sharedApplication().idleTimerDisabled = true

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

More about UIApplication Class.