Created
December 17, 2010 01:31
-
-
Save marshluca/744339 to your computer and use it in GitHub Desktop.
按住Home后,退出保存数据
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)applicationWillResignActive:(UIApplication *)application { | |
/* | |
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. | |
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. | |
*/ | |
NSLog(@"applicationWillResignActive"); | |
UIApplication *app = [UIApplication sharedApplication]; | |
BOOL backgroundSupported = NO; | |
// 判断是否支持多任务 | |
UIDevice* device = [UIDevice currentDevice]; | |
if ([device respondsToSelector:@selector(isMultitaskingSupported)]) { | |
backgroundSupported = device.multitaskingSupported; | |
} | |
if(backgroundSupported==NO) { // 不支持多任务 | |
[[NSNotificationCenter defaultCenter ] addObserver:self | |
selector:@selector(applicationWillTerminate:) | |
name:UIApplicationWillTerminateNotification | |
object:app]; | |
} else { // 表示支持多任务 | |
/*这个新的UIApplicationWillResignActiveNotification就是在用户按下home键时的通知, | |
建议用它的selector去保存数据,能防止用户直接在任务管理器里强制退出~*/ | |
[[NSNotificationCenter defaultCenter ] addObserver:self | |
selector:@selector(applicationDidEnterBackground:) | |
name:UIApplicationWillResignActiveNotification | |
object:app]; | |
/*这个UIApplicationWillEnterForegroundNotification是app在切回前台时的通知,可以显示“欢迎回来”之类的*/ | |
[[NSNotificationCenter defaultCenter ] addObserver:self | |
selector:@selector(applicationDidBecomeActive:) | |
name:UIApplicationWillEnterForegroundNotification | |
object:app]; | |
} | |
} | |
- (void)applicationDidBecomeActive:(UIApplication *)application { | |
/* | |
Restart any tasks that were paused (or not yet started) while the application was inactive. | |
*/ | |
NSLog(@"applicationDidBecomeActive"); | |
} | |
- (void)applicationDidEnterBackground:(UIApplication *)application { | |
/* | |
Called when the application entered background. | |
*/ | |
NSLog(@"applicationDidEnterBackground"); | |
// save local data here when suppourt mutitasks | |
} | |
- (void)applicationWillTerminate:(UIApplication *)application { | |
/* | |
Called when the application is about to terminate. | |
*/ | |
NSLog(@"applicationWillTerminate"); | |
// save local data here when not suppourt mutitasks | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment