使用通知:
当应用程序进入后台时会发布以下通知。
UIApplicationDidEnterBackgroundNotification
当应用程序变为活动状态时,会发布以下通知。
UIApplicationDidBecomeActiveNotification
在函数中添加通知观察者
//Adding observer for notification
-(void)viewDidLoad
{
// Your other code goes here
// Adding observer for notification when application entered the background
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackgroundNotification:) name:UIApplicationDidEnterBackgroundNotification object:nil];
}
// This method will be called when application entered in background
-(void)applicationDidEnterBackgroundNotification:(id)sender
{
// Do whatever you want when application in background
}