ios - 每次用户使用应用程序时记录时间戳

标签 ios

我想在用户打开应用程序时记录时间戳。现在,我可以在 ViewDidLoad 方法中进行记录,但是,一旦用户关闭应用程序并转到后台,然后再次打开应用程序,我将无法获取时间戳,因为ViewDidLoad 不再运行。你们中有人知道将我的时间戳代码放在哪里吗?

最佳答案

您可以将其放在 - (void)applicationDidBecomeActive:(UIApplication *)application 内,但这不是一个好方法,因为启动应用程序的用户并不是唯一会执行此操作的事件。导致保存时间戳,因为在其他情况下可能会调用此方法。

来自苹果的Documentation

applicationDidBecomeActive: Tells the delegate that the application has become active.

This method is called to let your application know that it moved from the inactive to active state. This can occur because your application was launched by the user or the system. Applications can also return to the active state if the user chooses to ignore an interruption (such as an incoming phone call or SMS message) that sent the application temporarily to the inactive state.

正确的做法是将存储时间戳的代码都放在 application:didFinishLaunchingWithOptions:applicationWillEnterForeground: 中 第一个方法将在第一次启动时调用,第二个方法将在应用程序从后台启动时调用。由于忽略的中断不会将应用程序发送到后台,因此在这种情况下不会调用 applicationWillEnterForeground,因此在用户忽略电话调用后,您不会存储时间戳。

检查this answer ,因为它很好地总结了处理将应用程序发送到后台并将其返回前台所涉及的方法的作用以及每种方法的用途。

关于ios - 每次用户使用应用程序时记录时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17903779/

相关文章:

iphone - 如何清除 CGContextRef?

ios - Zoomscale UIScrollView 不工作

ios - 如何在 SWIFT 的 Objective-C 中替换 #define 宏?

ios - 删除了 XIB,应用程序有时会崩溃

ios - 使用 loadNibNamed 和 Size Classes 获取约束

iOS - NSInternalInconsistencyException 'circular dependency when saving'

iphone - 为什么 UINavigationController 的 toolbarItems 在 View 的其余部分之后刷新 4-5 秒?

ios - 如何在 iOS 中将 Firebase 对象持久保存到磁盘?

ios - 从左到右更改导航标题位置

ios - 在后台运行 ios 应用程序