I'm developing an music/video player app and just need to konw how to disable the auto-lock while my app is in foreground.
I know I've to use [[UIApplication sharedApplication] setIdleTimerDisabled:YES];
and [[UIApplication sharedApplication] setIdleTimerDisabled:NO];
at some point, but where is the best place to put them?
A setting called Guided Access keeps an iPhone or iPad locked in an app, even if someone hits the home button. To turn on the feature, tap on Settings -> General -> Accessibility -> Guided Access, and turn flip the switch so it looks green. (Don't worry, your phone won't function any differently unless prompted.)
Enable the idle timer in
- (void)applicationWillResignActive:(UIApplication *)application
and disable it in
- (void)applicationDidBecomeActive:(UIApplication *)application
The best place to disable it is in didFinishLaunchingWithOptions. The system will automatically take care of making the setting have no effect when the app is in the background.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { application.idleTimerDisabled = YES; return YES; }
I posted this alternative because the accepted answer does not prevent auto-lock when an alert appears (email, message, calendar event etc), or the notification center or control center is up.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With