繁体   English   中英

什么时候应该存储并重新存储到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)
}

我应该只在其中一些存储\\还原吗? 我应该何时断开连接并重新连接到GCM服务?

我的恢复是多余的?

保持一个本地标志说是恢复已经做了不实用,因为应用程序可能会破坏它?

在Apple的iOS应用程序编程指南中了解应用程序生命周期:应用程序的执行状态

此外, UIApplicationDelegate针对您在问题中提到的方法的文档在调用时包含非常详细的信息。 applicationWillResignActive的文档为例。

  • didEnterBackground总是以willResignActive didEnterBackground ,因此不需要运行相同的代码。

  • willEnterForeground总是后跟didBecomeActive ,但在其他情况下也可以调用didBecomeActive (见下文)。

  • 可以在没有调用didEnterBackground情况下调用willResignActive ,例如10%的电池警告或电话呼叫。 如果用户拒绝该呼叫,您的应用将保持在前台,并且接下来会调用didBecomeActive以告诉您该应用再次处于活动状态。

  • willTerminate永远不会在现代iOS应用程序中调用。 在iOS支持多任务处理之前,它在iOS 2和3中使用。 由于应用程序现在在用户“退出”时移至后台,因此不再使用此事件。 (当操作系统因后台处于内存压力而导致你的应用程序被杀死时,它会立即被杀死而不再执行任何代码。)

综上所述:

  • stopTasks / startTasks应该在willResignActivedidBecomeActive
  • 保存/恢复用户数据可以在willResignActive / didBecomeActivedidEnterBackground / willEnterForeground 我可能会选择后者。

暂无
暂无

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

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