双击home后强制退出保存数据
在双击home后强制退出,导致保存失败。
其实苹果有一个UIApplicationWillResignActiveNotification通知:
看代码吧:
//首先声明app
UIApplication * app= [UIApplication sharedApplication];
//接下来四行是新的功能,能检查设备是否支持后台,老系统和一代的backgroundSupported = NO;支持的自然就是YES啦
UIDevice* device = [UIDevice currentDevice];
BOOL backgroundSupported = NO;
if ([device respondsToSelector:@selector(isMultitaskingSupported)])
backgroundSupported = device.multitaskingSupported;
//如果设备不支持后台,那么就继续使用applicationWillTerminate:
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];
}

浙公网安备 33010602011771号