我在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服务?
我的恢复是多余的吗?
保持本地标志说恢复已经不可行,因为应用程序可能会破坏它?
答案 0 :(得分:2)
在Apple的iOS应用编程指南中阅读The App Lifecycle: Execution States for Apps。
此外,UIApplicationDelegate
中有关您在问题中提及的方法的文档在调用时包含非常详细的信息。以the documentation for applicationWillResignActive
为例。
didEnterBackground
始终以willResignActive
开头,因此无需运行相同的代码。
willEnterForeground
后面跟着didBecomeActive
,但在其他情况下也可以调用didBecomeActive
(见下文)。
willResignActive
可以在不调用 didEnterBackground
的情况下调用,例如10%的电池警告或电话来了。如果用户拒绝来电,您的应用将保留在前台,然后会调用didBecomeActive
,告诉您应用再次处于有效状态。
willTerminate
永远不会在现代iOS应用中调用。在iOS支持多任务处理之前,它在iOS 2和3中使用。由于应用程序现在在用户“退出”时移至后台,因此不再使用此事件。 (当操作系统因后台处于内存压力而导致你的应用程序被杀死时,它会立即被杀死而不再执行任何代码。)
总结:
stopTasks
/ startTasks
应位于willResignActive
和didBecomeActive
。willResignActive
/ didBecomeActive
或didEnterBackground
/ willEnterForeground
中保存/恢复用户数据。我可能会选择后者。