我什么时候应该在 ios swift 上存储和重新存储到钥匙串?

2023-12-24

我在 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 服务?

我的恢复是多余的吗?

保留一个本地标志表示已进行恢复是不切实际的,因为应用程序可能会破坏它?


阅读应用程序生命周期:应用程序的执行状态 https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html#//apple_ref/doc/uid/TP40007072-CH2-SW3Apple 的 iOS 应用程序编程指南。

另外,文档中UIApplicationDelegate因为您在问题中提到的方法在调用时包含非常详细的信息。拿的文档applicationWillResignActive https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html#//apple_ref/occ/intfm/UIApplicationDelegate/applicationWillResignActive:举个例子。

  • didEnterBackground总是前面有willResignActive,因此无需运行相同的代码。

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

  • willResignActive可以称为without didEnterBackground被称为,例如电量不足 10% 时发出警告或接到电话。如果用户拒绝来电,您的应用程序将保留在前台,并且didBecomeActive接下来调用,告诉您该应用程序再次处于活动状态。

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

总之:

  • stopTasks/startTasks应该在willResignActive and didBecomeActive.
  • 保存/恢复用户数据可以在willResignActive/didBecomeActive or didEnterBackground/willEnterForeground。我可能会选择后者。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

我什么时候应该在 ios swift 上存储和重新存储到钥匙串? 的相关文章

  • Swift 中的 import 语句是否有相关成本?

    阅读字符串宣言 我看到一个段落 https github com apple swift blob master docs StringManifesto md batteries included关于避免Foundation不需要的时候导
  • 在 UIScrollview 上显示缩略图的最佳方法是什么(从服务器下载)

    我想在 UIScrollview 如照片应用程序 上显示许多图像 作为缩略图 所有图像将从服务器下载 据我所知 有几种选择 1 通过创建 UIImageviews 然后将它们添加为主滚动视图上的子视图 2 通过子类化一个UIView类 然后
  • iOS - UITableViewCell 使文本加粗

    我有一个字符串 NSString userInfo James Johnson james 我想做的就是大胆James Johnson并保留 james正常字体 所以我尝试过的是使用NSAttributedString但为了完成这个过程 我
  • Apple Mach-O 链接器错误(静态,不是 ld)

    我最近遇到了 Apple Mach O 链接器错误 大多数指南建议将 构建设置 中的位码更改为 否 但它仅适用于 ld 错误 这与我的不同 我会提供截图 请帮忙修复bug pod HandySwift 导致了错误的出现 这是它的 Githu
  • 将 UIToolBar 添加到所有键盘(swift)

    我正在尝试以尽可能少的重复次数将自定义 UIToolBar 添加到我的所有键盘中 我目前的做法要求我将代码添加到所有 viewDidLoads 中 并将每个文本字段的委托分配给我正在使用的 viewController 我尝试创建自己的 U
  • 如何动态获取 UITableViewCell 的高度

    我创建了自定义的tableViewCell 我在UITableViewCell中添加了UIView SubView 所以我在 UIView 中的所有动态文本和图像内容都会根据文本和图像大小而变化 但现在 HeightforRowAtInde
  • 无法在 ios 应用程序中通过 googlecast 正确投射视频

    我正在开发一个基于 AVPlayer 的自定义视频播放器项目 尝试整合谷歌演员 我已经根据谷歌图进行了集成 https codelabs developers google com codelabs cast videos ios http
  • 从未调用过交互式委托方法

    我想在 ViewController 1 和 NavigationViewController 2 之间进行交互式转换 NavigationController 通过按钮调用 因此呈现时没有交互转换 它可以通过按钮或 UIPanGestur
  • 如何为 iPhone 6+、6 和 5 指定不同尺寸?

    我想让 iPhone 6 6 和 5 上的视图看起来几乎相同 在附图中 我的意思是 例如 取消 按钮在 iPhone 5 中距离屏幕左边缘应为 30 像素 在 6 中为 35 像素 在 6 中为 45 像素 其他元素也类似 如何为每种类型设
  • BigQuery 未显示链接的 Firebase Analytics 事件日志的任何数据集

    我将我的帐户链接到 Big Query 但 Firebase Analytics 事件不会自动加载到 BigQuery 中 显示 未找到数据集 警告 我的工作进度附在下面 请查收 I have getting firebase Analyt
  • 将类型传递给通用 Swift 扩展,或者理想情况下推断它

    说你有 class Fancy UIView 你想找到所有兄弟姐妹Fancy意见 没问题 https stackoverflow com q 37232743 294884 for v UIView in superview subview
  • Swift:长按手势识别器 - 检测轻击和长按

    我想连接一个动作 如果手势是点击 它会以特定的方式为对象设置动画 但如果按下持续时间超过 0 5 秒 它会执行其他操作 现在 我刚刚连接了动画 我不知道如何区分长按和点击 如何访问新闻持续时间以实现上述目的 IBAction func ta
  • 如何删除 UITableView 中的缩进?

    首先 我对此很陌生 我很可能忘记了一些非常简单的事情 问题 我正在制作一个应用程序 在 a 中显示来自 imgur com 的随机图像tableView 由于某种原因 所有单元格都会缩进少量 如下图所示 我摆弄了许多设置storyboard
  • 在 WKWebView 中禁用缩放?

    有谁知道在 WKWebView 中禁用双击和捏缩放的简单方法 我尝试过的任何方法都不起作用 Webview scrollView allowsMagnification false Error value of type WKWebView
  • 如何使用phonegap在iOS应用程序中防止键盘推送webview

    当屏幕底部的输入字段获得焦点时 键盘会向上推我的网络视图 并且页面的上部不再可见 我想防止键盘推高网络视图 有人有主意吗 对焦 设置window scrollTo 0 0 这可以防止键盘完全推高 webview input on focus
  • 关闭捕获上下文 Swift

    当我尝试更改闭包中的变量时出现此错误 A C function pointer cannot be formed from a closure that captures context 是否有解决方法或者仍然可以更改闭包内的变量 My C
  • 使用输入类型 = 文件捕获照片时移动 safari 崩溃

    我正在使用输入类型文件在 iOS 中启动相机 我正在使用以下代码行
  • 诊断和仪器均缺少“僵尸”选项

    运行 Xcode 4 0 2 Zombie 选项丢失 其他 SO 帖子建议找到它的两个地方 Product gt Run looks like this Product gt Profile looks like this 奇怪的是 我之前
  • Mac 上的 Delphi - 可能吗? [关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我负责一个 Delphi Win32 项目管理应用程序 我刚刚完成了向 Delphi 2009 的迁移
  • Unwind segue 的用途是什么以及如何使用它们?

    iOS 6 和 Xcode 4 5 有一个称为 Unwind Segue 的新功能 展开转场可以允许过渡到故事板中场景的现有实例 除了 Xcode 4 5 发行说明中的 这个简短条目之外 UIViewController 现在似乎还有几个新

随机推荐