我在 appDelegate 中看到了一些方法,我不确定仅在其中一些方法中存储和重新存储用户状态是否涵盖了所有场景?
func applicationWillResignActive(application: UIApplication) {
// 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.
stopTasks()
setSharedPrefrences()
}
func applicationDidEnterBackground(application: UIApplication) {
// 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.
stopTasks()
setSharedPrefrences()
}
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
startTasks()
getSharedPrefrences()
}
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
startTasks()
getSharedPrefrences()
connectGcmService(application)
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
stopTasks()
setSharedPrefrences()
disconnectGcmService(application)
}
我应该只存储\恢复其中的一些吗?
我应该什么时候断开并重新连接到 GCM 服务?
我的恢复是多余的吗?
保留一个本地标志表示已进行恢复是不切实际的,因为应用程序可能会破坏它?
阅读应用程序生命周期:应用程序的执行状态 https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html#//apple_ref/doc/uid/TP40007072-CH2-SW3Apple 的 iOS 应用程序编程指南。
另外,文档中UIApplicationDelegate
因为您在问题中提到的方法在调用时包含非常详细的信息。拿的文档applicationWillResignActive https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html#//apple_ref/occ/intfm/UIApplicationDelegate/applicationWillResignActive:举个例子。
didEnterBackground
总是前面有willResignActive
,因此无需运行相同的代码。
willEnterForeground
总是跟随didBecomeActive
, but didBecomeActive
也可以在其他情况下调用(见下文)。
willResignActive
可以称为without didEnterBackground
被称为,例如电量不足 10% 时发出警告或接到电话。如果用户拒绝来电,您的应用程序将保留在前台,并且didBecomeActive
接下来调用,告诉您该应用程序再次处于活动状态。
willTerminate
在现代 iOS 应用程序中从未被调用。在 iOS 支持多任务之前,它已在 iOS 2 和 3 中使用。由于现在的应用程序在用户“退出”它们时会移至后台,因此不再使用此事件。 (当操作系统由于内存压力而杀死您的应用程序时,它会立即被杀死,而不会执行任何更多代码。)
总之:
-
stopTasks
/startTasks
应该在willResignActive
and didBecomeActive
.
- 保存/恢复用户数据可以在
willResignActive
/didBecomeActive
or didEnterBackground
/willEnterForeground
。我可能会选择后者。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)