1. 注册通知
//后台进前台通知 UIApplicationDidBecomeActiveNotification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
//进入后台UIApplicationDidEnterBackgroundNotification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
2. 实现通知方法
//进入后台方法
- (void)didEnterBackground {
NSLog(@"%@", NSStringFromSelector(_cmd));
}
//每次后台进前台都会执行这个方法
- (void)didBecomeActive {
NSLog(@"%@", NSStringFromSelector(_cmd));
}
3. 移除通知
- (void)dealloc {
[[NSNotificationCenter defaultCenter]removeObserver:self]; //移除通知
}