Detect when application is minimized (iOS)

I am creating a mobile game and I have NSTimer for my stopwatch. Everything works fine, except when I minimize the application (press the main screen), or when it is interrupted, for example, by phone, NSTimer continues to run in the background when the application is not in use.

I need to cancel this timer when the application is minimized / interrupted, and create a new timer when the application resumes. What methods are processed when the application is minimized and resumed?

+4
source share
1 answer

You can invalidate the timer in AppDelegate in the UIApplication applicationDidEnterBackground delegation method this way

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

And create a new timer in the applicationWillEnterForeground method

+6
source

All Articles