Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable iPhone/iPad auto-lock while app is in foreground mode?

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?

like image 333
jMelnik Avatar asked Feb 23 '12 12:02

jMelnik


People also ask

How do I lock an app from closing on my iPad?

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


2 Answers

Enable the idle timer in

- (void)applicationWillResignActive:(UIApplication *)application 

and disable it in

- (void)applicationDidBecomeActive:(UIApplication *)application 
like image 185
Era Avatar answered Sep 29 '22 11:09

Era


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.

like image 21
malhal Avatar answered Sep 29 '22 10:09

malhal