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?
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
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