Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

People also ask

Can you override Sleep Mode on iPhone?

Go to Settings > Focus and tap any of the categories, like Do Not Disturb, Driving, or Sleep. Under Allowed Notifications, you can choose settings separately for People and Apps. Tap People and you can find the old option for passing through calls, now under Also Allow as Calls From.

How do I turn off Sleep Mode on iOS 14?

Tip: You can turn off iPhone Sleep Mode on iPhone manually from Control Center as well. Just to do so, open Settings > Control Center and add Sleep Mode to the list of included controls. Now, toggle Sleep Mode off or on anytime in one tap directly from the Control Center.

How do I change the Sleep settings on iOS?

Tap Browse at the bottom right, then tap Sleep. Scroll down to Your Schedule, then tap Full Schedule & Options. Tap Sleep Goal, then select a time.


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.


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).


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
 }