How to disable iPhone/iPad auto-lock while app is in foreground mode?
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.
Swift 3.0:
Inside AppDelegate.swift:application.idleTimerDisabled = true
Outside AppDelegate.swift:UIApplication.shared().isIdleTimerDisabled = true