第一步:
創(chuàng)建2個(gè)NSNotificationCenter監(jiān)聽
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:)
name:UIApplicationWillResignActiveNotification object:nil]; //監(jiān)聽是否觸發(fā)home鍵掛起程序.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification object:nil]; //監(jiān)聽是否重新進(jìn)入程序程序.
第二步:
實(shí)現(xiàn)2個(gè)NSNotificationCenter所觸發(fā)的事件方法
- (void)applicationWillResignActive:(NSNotification *)notification
{
printf("按理說是觸發(fā)home按下\n");
}
- (void)applicationDidBecomeActive:(NSNotification *)notification
{
printf("按理說是重新進(jìn)來后響應(yīng)\n");
}
注: 在home鍵觸發(fā)后,AppDelegate響應(yīng)的方法為:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
/*
Use this method to release shared resources, save user data, invalidate timers,
and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
*/
}