Keep iphone active while running program
Asked Answered
A

3

37

how to set iPhone device to stay active ( to not lock ) while my app is running ? Any idea

Allegra answered 29/1, 2009 at 21:23 Comment(0)
L
50

This code will prevent your iPhone from going to sleep while your app is running

// avoid sleeping when this application is running
UIApplication *application = [UIApplication sharedApplication];
application.idleTimerDisabled = YES;
// Or simpler
[[UIApplication sharedApplication] setIdleTimerDisabled: YES];
Laskowski answered 29/1, 2009 at 21:28 Comment(1)
Note to anyone reading this accepted answer: this will only work if you have an application object already, which isn't common. Instead, you should use [UIApplication sharedApplication].idleTimerDisabled = YES;Strafe
H
78

I'm not sure if this prevents the device from locking, but you can prevent the screen from dimming with the UIApplication's idleTimerDisabled property:

[UIApplication sharedApplication].idleTimerDisabled = YES;

From the documentation:

Important: You should set this property only if necessary and should be sure to reset it to NO when the need no longer exists. Most applications should let the system turn off the screen when the idle timer elapses. This includes audio applications. With appropriate use of Audio Session Services, playback and recording proceed uninterrupted when the screen turns off. The only applications that should disable the idle timer are mapping applications, games, or similar programs with sporadic user interaction.

Hoedown answered 29/1, 2009 at 21:28 Comment(0)
L
50

This code will prevent your iPhone from going to sleep while your app is running

// avoid sleeping when this application is running
UIApplication *application = [UIApplication sharedApplication];
application.idleTimerDisabled = YES;
// Or simpler
[[UIApplication sharedApplication] setIdleTimerDisabled: YES];
Laskowski answered 29/1, 2009 at 21:28 Comment(1)
Note to anyone reading this accepted answer: this will only work if you have an application object already, which isn't common. Instead, you should use [UIApplication sharedApplication].idleTimerDisabled = YES;Strafe
S
6

If you landed here looking for an answer in Swift, it's this:

UIApplication.sharedApplication().idleTimerDisabled = true

for Swift 3

UIApplication.shared.isIdleTimerDisabled = true

The warning in this comment still applies.

Strafe answered 14/12, 2015 at 9:13 Comment(1)
Swift 3: UIApplication.shared.isIdleTimerDisabled = trueIrwin

© 2022 - 2024 — McMap. All rights reserved.