我应该什么时候存储和重新存储到 ios swift 上的钥匙串?
when should i store and re-store to keychain on ios swift?
我在 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)
}
我应该只 store\restore 中的一部分吗?
我应该什么时候断开并重新连接到 GCM 服务?
我的还原是多余的吗?
保持一个本地标志说恢复已经完成是不切实际的,因为应用程序可能会破坏它?
在 Apple 的 iOS 应用程序编程指南中阅读有关 The App Lifecycle: Execution States for Apps 的内容。
此外,UIApplicationDelegate
中有关您在问题中提到的方法的文档在调用时包含非常详细的信息。以the documentation for applicationWillResignActive
为例。
didEnterBackground
前面总是有willResignActive
,所以没必要运行一样的代码。
willEnterForeground
后面总是跟着didBecomeActive
,但是didBecomeActive
也可以在其他情况下调用(见下文)。
willResignActive
可以调用 而无需调用 didEnterBackground
,例如出现 10% 电量警告或 phone 来电。如果用户拒绝呼叫,您的应用程序将保留在前台,然后 didBecomeActive
会被调用以告诉您该应用程序再次激活。
willTerminate
在现代 iOS 应用程序中永远不会被调用。在 iOS 支持多任务处理之前,它被用在 iOS 2 和 3 中。由于现在的应用程序在用户 "quits" 时移动到后台,因此不再使用此事件。 (当 OS 在后台由于内存压力而杀死你的应用程序时,它会立即被杀死而不会执行任何更多代码。)
总结:
stopTasks
/startTasks
应该在 willResignActive
和 didBecomeActive
.
- Saving/restoring用户数据可以在
willResignActive
/didBecomeActive
或didEnterBackground
/willEnterForeground
中完成。我可能会选择后者。
我在 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)
}
我应该只 store\restore 中的一部分吗? 我应该什么时候断开并重新连接到 GCM 服务?
我的还原是多余的吗?
保持一个本地标志说恢复已经完成是不切实际的,因为应用程序可能会破坏它?
在 Apple 的 iOS 应用程序编程指南中阅读有关 The App Lifecycle: Execution States for Apps 的内容。
此外,UIApplicationDelegate
中有关您在问题中提到的方法的文档在调用时包含非常详细的信息。以the documentation for applicationWillResignActive
为例。
didEnterBackground
前面总是有willResignActive
,所以没必要运行一样的代码。willEnterForeground
后面总是跟着didBecomeActive
,但是didBecomeActive
也可以在其他情况下调用(见下文)。willResignActive
可以调用 而无需调用didEnterBackground
,例如出现 10% 电量警告或 phone 来电。如果用户拒绝呼叫,您的应用程序将保留在前台,然后didBecomeActive
会被调用以告诉您该应用程序再次激活。willTerminate
在现代 iOS 应用程序中永远不会被调用。在 iOS 支持多任务处理之前,它被用在 iOS 2 和 3 中。由于现在的应用程序在用户 "quits" 时移动到后台,因此不再使用此事件。 (当 OS 在后台由于内存压力而杀死你的应用程序时,它会立即被杀死而不会执行任何更多代码。)
总结:
stopTasks
/startTasks
应该在willResignActive
和didBecomeActive
.- Saving/restoring用户数据可以在
willResignActive
/didBecomeActive
或didEnterBackground
/willEnterForeground
中完成。我可能会选择后者。