How can I prevent the display on an iOS device from dimming and turning off?

IphoneIpadScreenIos

Iphone Problem Overview


I'm diving into iOS development and am building my own alarm clock app to become familiar with platform and SDK. I've noticed some alarm clock apps in the app store keep the screen from dimming and/or turning off when their app is running. How is this implemented?

Iphone Solutions


Solution 1 - Iphone

Add this line of code on your application delegate:

Objective-C:

[[UIApplication sharedApplication] setIdleTimerDisabled:YES];

Swift 2:

UIApplication.sharedApplication().idleTimerDisabled = true

Swift 3, 4 & 5:

UIApplication.shared.isIdleTimerDisabled = true

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBeachRunnerFredView Question on Stackoverflow
Solution 1 - IphoneIñigo BeitiaView Answer on Stackoverflow