简体   繁体   English

什么时候应该存储并重新存储到ios swift上的钥匙串?

[英]when should i store and re-store to keychain on ios swift?

I see in the appDelegate few methods and I'm not sure if storing and re-storing user state in just some of them covers all scenarios? 我在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)
}

should I store\\restore in only some of them? 我应该只在其中一些存储\\还原吗? when should I disconnect and reconnect to GCM service? 我应该何时断开连接并重新连接到GCM服务?

are my restore redundant? 我的恢复是多余的?

Keeping a local flag saying is restore has been made is not practical, as the application might destroy it? 保持一个本地标志说是恢复已经做了不实用,因为应用程序可能会破坏它?

Read about The App Lifecycle: Execution States for Apps in Apple's App Programming Guide for iOS. 在Apple的iOS应用程序编程指南中了解应用程序生命周期:应用程序的执行状态

Also, the documentation in UIApplicationDelegate for the methods you mention in your question contains very detailed information when they are called. 此外, UIApplicationDelegate针对您在问题中提到的方法的文档在调用时包含非常详细的信息。 Take the documentation for applicationWillResignActive as an example. applicationWillResignActive的文档为例。

  • didEnterBackground is always preceded by willResignActive , so there is no need to run the same code. didEnterBackground总是以willResignActive didEnterBackground ,因此不需要运行相同的代码。

  • willEnterForeground is always followed by didBecomeActive , but didBecomeActive can also be called in other situations (see below). willEnterForeground总是后跟didBecomeActive ,但在其他情况下也可以调用didBecomeActive (见下文)。

  • willResignActive can be called without didEnterBackground being called, eg a 10% battery warning or a phone call comes. 可以在没有调用didEnterBackground情况下调用willResignActive ,例如10%的电池警告或电话呼叫。 If the user declines the call, your app will remain in the foreground and didBecomeActive is called next to tell you that the app is active again. 如果用户拒绝该呼叫,您的应用将保持在前台,并且接下来会调用didBecomeActive以告诉您该应用再次处于活动状态。

  • willTerminate is never called in modern iOS apps. willTerminate永远不会在现代iOS应用程序中调用。 It was used in iOS 2 and 3 before iOS supported multitasking. 在iOS支持多任务处理之前,它在iOS 2和3中使用。 Since apps nowadays move to the background when the user "quits" them, this event is no longer used. 由于应用程序现在在用户“退出”时移至后台,因此不再使用此事件。 (When the OS kills your app due to memory pressure while it is in the background, it gets killed right away without executing any more code.) (当操作系统因后台处于内存压力而导致你的应用程序被杀死时,它会立即被杀死而不再执行任何代码。)

In summary: 综上所述:

  • stopTasks / startTasks should be in willResignActive and didBecomeActive . stopTasks / startTasks应该在willResignActivedidBecomeActive
  • Saving/restoring user data can be done in willResignActive / didBecomeActive or didEnterBackground / willEnterForeground . 保存/恢复用户数据可以在willResignActive / didBecomeActivedidEnterBackground / willEnterForeground I would probably choose the latter. 我可能会选择后者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM