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

假如想象 提交于 2019-12-04 10:49:13

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