Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect when app is minimized (iOS)

I am building a mobile game and I have a NSTimer for my stopwatch. Everything works fine, except when I minimize the app (press home screen), or when it is interrupted by, say a phone call, the NSTimer continues running in the background, when the application is not in use.

I need to invalidate this timer when the app is minimized/interrupted and create a new timer when the app is resumed. What methods handle when the app is minimized and resumed?

like image 736
Josh O'Connor Avatar asked Mar 01 '16 07:03

Josh O'Connor


1 Answers

You can invalidate timer in AppDelegate in UIApplication delegate method applicationDidEnterBackground this way,

- (void)applicationDidEnterBackground:(UIApplication *)application {
    [timer invalidate];
    timer = nil;
}

And create a new timer in method applicationWillEnterForeground

like image 169
Alap Anerao Avatar answered Sep 20 '22 10:09

Alap Anerao