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

BeachRunnerFred picture BeachRunnerFred · Aug 8, 2010 · Viewed 34.6k times · Source

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?

Answer

Iñigo Beitia picture Iñigo Beitia · Aug 9, 2010

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