简体   繁体   English

自从上次在iOS上打开应用程序多久以来?

[英]How long since last time app was opened in iOS?

How can I tell the time from the last time my app was open? 如何判断我的应用程序上次打开时的时间? Can this still be monitored even my app is not running in the background? 即使我的应用程序没有在后台运行,这仍然可以监控吗?

Thanks. 谢谢。

Put something like 放点像

[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"kLastCloseDate"];

in both 同时

- (void)applicationWillTerminate:(UIApplication *)application
- (void)applicationDidEnterBackground:(UIApplication *)application

Then check the difference at startup: 然后检查启动时的差异:

NSDate *lastDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"kLastCloseDate"];
NSTimeInterval timeDiff = [[NSDate date] timeIntervalSinceDate:lastDate];
// your stuff

in both 同时

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- (void)applicationWillEnterForeground:(UIApplication *)application

In your application delegates - (void)applicationDidEnterBackground:(UIApplication *)application method, just write the current time to a file. 在您的应用程序委托- (void)applicationDidEnterBackground:(UIApplication *)application方法中,只需将当前时间写入文件即可。 Then, in either applicationWillEnterForeground: or applicationDidFinishLaunching , read this file and compare with the current time. 然后,在applicationWillEnterForeground:applicationDidFinishLaunching ,读取此文件并与当前时间进行比较。 The difference will tell you how long since your application was last foremost. 差异将告诉您自应用程序最后一次以来有多长时间。

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

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