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

五迷三道 提交于 2019-12-06 07:03:28

问题


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?

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?

are my restore redundant?

Keeping a local flag saying is restore has been made is not practical, as the application might destroy it?


回答1:


Read about The App Lifecycle: Execution States for Apps in Apple's App Programming Guide for iOS.

Also, the documentation in UIApplicationDelegate for the methods you mention in your question contains very detailed information when they are called. Take the documentation for applicationWillResignActive as an example.

  • didEnterBackground is always preceded by willResignActive, so there is no need to run the same code.

  • willEnterForeground is always followed by didBecomeActive, but didBecomeActive can also be called in other situations (see below).

  • willResignActive can be called without didEnterBackground being called, e.g. a 10% battery warning or a phone call comes. 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.

  • willTerminate is never called in modern iOS apps. It was used in iOS 2 and 3 before iOS supported multitasking. 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.
  • Saving/restoring user data can be done in willResignActive/didBecomeActive or didEnterBackground/willEnterForeground. I would probably choose the latter.


来源:https://stackoverflow.com/questions/35360453/when-should-i-store-and-re-store-to-keychain-on-ios-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!