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

