您必须收听以下两个通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appLostFocus:) name:UIApplicationWillResignActiveNotification object:[UIApplication sharedApplication]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appGotFocus:) name:UIApplicationDidBecomeActiveNotification object:[UIApplication sharedApplication]];
并妥善处理:
- (void)appLostFocus:(NSNotification*)notification
{
NSLog(@"app lost focus");
}
- (void)appGotFocus:(NSNotification*)notification
{
NSLog(@"app got focus");
}
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
//...
}