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

jos*_*im5 8 iphone time objective-c ipad ios

如何判断我的应用程序上次打开时的时间?即使我的应用程序没有在后台运行,这仍然可以监控吗?

谢谢.

Jil*_*ouc 18

放点像

[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"kLastCloseDate"];
Run Code Online (Sandbox Code Playgroud)

同时

- (void)applicationWillTerminate:(UIApplication *)application
- (void)applicationDidEnterBackground:(UIApplication *)application
Run Code Online (Sandbox Code Playgroud)

然后检查启动时的差异:

NSDate *lastDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"kLastCloseDate"];
NSTimeInterval timeDiff = [[NSDate date] timeIntervalSinceDate:lastDate];
// your stuff
Run Code Online (Sandbox Code Playgroud)

同时

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- (void)applicationWillEnterForeground:(UIApplication *)application
Run Code Online (Sandbox Code Playgroud)