iOS development
sleep mode
programming tutorial
app development
mobile programming

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

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In iOS, controlling screen sleep is done through the idle timer setting. Apps such as navigation, media playback, workouts, or kiosk workflows often need to keep the screen awake temporarily. The key is enabling this only when needed and restoring default behavior promptly to protect battery life.

Use isIdleTimerDisabled Correctly

The API is simple: set UIApplication.shared.isIdleTimerDisabled to true to prevent auto-lock while your app is active.

swift
1import UIKit
2
3final class ReaderViewController: UIViewController {
4    override func viewDidAppear(_ animated: Bool) {
5        super.viewDidAppear(animated)
6        UIApplication.shared.isIdleTimerDisabled = true
7    }
8
9    override func viewWillDisappear(_ animated: Bool) {
10        super.viewWillDisappear(animated)
11        UIApplication.shared.isIdleTimerDisabled = false
12    }
13}

This basic pattern works for many single-screen cases, but larger apps benefit from centralized control.

Centralize State with a Manager

If multiple features may request keep-awake mode, use a reference-count manager so one feature does not accidentally re-enable sleep while another still needs it.

swift
1import UIKit
2
3final class IdleTimerManager {
4    static let shared = IdleTimerManager()
5
6    private var holders = Set<String>()
7
8    private init() {}
9
10    func acquire(holder: String) {
11        holders.insert(holder)
12        applyState()
13    }
14
15    func release(holder: String) {
16        holders.remove(holder)
17        applyState()
18    }
19
20    private func applyState() {
21        UIApplication.shared.isIdleTimerDisabled = !holders.isEmpty
22    }
23}
24
25// Usage:
26// IdleTimerManager.shared.acquire(holder: "video-player")
27// IdleTimerManager.shared.release(holder: "video-player")

This avoids race conditions in apps with playback, navigation, and camera features running in different flows.

Handle Scene and App Lifecycle

Sleep control should align with foreground state. If the app enters background, iOS behavior changes and keeping the timer disabled is not useful.

swift
1import UIKit
2
3final class SceneDelegate: UIResponder, UIWindowSceneDelegate {
4    func sceneDidEnterBackground(_ scene: UIScene) {
5        UIApplication.shared.isIdleTimerDisabled = false
6    }
7
8    func sceneWillEnterForeground(_ scene: UIScene) {
9        // Optional: restore based on active feature state
10        let shouldDisable = false
11        UIApplication.shared.isIdleTimerDisabled = shouldDisable
12    }
13}

Treat lifecycle transitions as reset points so state never remains accidentally locked.

Pair with User Intent and Battery Awareness

Do not disable sleep globally for an entire app session unless the experience truly requires it. Scope it to active tasks, such as turn-by-turn navigation while route guidance is visible.

Consider explicit UI cues when screen-awake mode is active. This helps users understand battery impact and avoids confusion when their device does not auto-lock.

If your feature can run with occasional sleep, prefer short user-configurable durations rather than permanent keep-awake mode.

Add Feature-Level Telemetry

When keep-awake mode is enabled, log lightweight telemetry events with feature id and duration. This helps detect flows that forget to release idle timer disable state. In QA environments, you can surface a debug banner showing whether idle timer is currently disabled and which holder requested it. Such visibility shortens debugging cycles when multiple teams own different screens. Telemetry is also useful for battery-impact analysis, since unexpectedly long keep-awake sessions often correlate with user complaints about power drain.

QA Checklist for Keep-Awake Flows

Test rotation, app switching, and interruption events while keep-awake mode is active. Confirm the setting resets correctly after dismissing the related feature and after app restarts.

Common Pitfalls

A common mistake is enabling idle timer disable in one controller and forgetting to restore it on dismissal. This leaves the whole app preventing sleep.

Another issue is toggling the value from multiple places without coordination. The last writer wins, which can break unrelated features.

Developers also assume this setting bypasses all lock behaviors. It only affects idle auto-lock while the app is active and visible.

Finally, avoid setting keep-awake mode for background-only tasks. It provides no benefit and adds maintenance complexity.

Summary

  • Use UIApplication.shared.isIdleTimerDisabled for temporary keep-awake behavior.
  • Scope changes to feature lifecycle and restore defaults promptly.
  • Use a centralized manager in multi-feature apps to prevent conflicts.
  • Align behavior with scene and foreground transitions.
  • Balance user experience needs against battery impact.

Course illustration
Course illustration