我什么时候应该在ios swift上存储和重新存储到钥匙串? [英] when should i store and re-store to keychain on ios swift?

查看:207
本文介绍了我什么时候应该在ios swift上存储和重新存储到钥匙串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在appDelegate中看到了几种方法,我不确定在其中某些方法中是否存储和重新存储用户状态是否涵盖所有情况?

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)
}

我应该只在其中一些中存储\恢复吗? 什么时候应该断开并重新连接到GCM服务?

should I store\restore in only some of them? when should I disconnect and reconnect to GCM service?

还原是否多余?

保留本地标志,表明已进行还原是不切实际的,因为应用程序可能会破坏它?

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

推荐答案

了解有关此外,UIApplicationDelegate中有关您在问题中提到的方法的文档包含了调用它们时的非常详细的信息.以applicationWillResignActive 的文档为例.

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始终以willResignActive开头,因此无需运行相同的代码.

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

willEnterForeground总是跟在didBecomeActive之后,但在其他情况下也可以调用didBecomeActive(请参见下文).

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

willResignActive而不调用 didEnterBackground,例如电池电量警告为10%或打来电话.如果用户拒绝呼叫,则您的应用程序将保留在前台,并在下一个呼叫didBecomeActive时告诉您该应用程序再次处于活动状态.

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.在iOS支持多任务处理之前,它已在iOS 2和3中使用.由于当今的应用程序在用户退出"时会移至后台,因此不再使用此事件. (当操作系统在后台因内存压力而杀死您的应用程序时,它会立即被杀死,而无需执行任何其他代码.)

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.)

总结:

  • stopTasks/startTasks应该位于willResignActivedidBecomeActive中.
  • 保存/恢复用户数据可以在willResignActive/didBecomeActivedidEnterBackground/willEnterForeground中完成.我可能会选择后者.
  • 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.

这篇关于我什么时候应该在ios swift上存储和重新存储到钥匙串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆