How long has it been since you last opened the app in iOS?

How can I find out the time since the last opening of my application? Is it possible to track, even my application is not running in the background?

Thanks.

+8
ios time objective-c iphone ipad
source share
2 answers

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 
+18
source share

In your application, it delegates the method - (void)applicationDidEnterBackground:(UIApplication *)application , just write the current time on the file. Then, in applicationWillEnterForeground: or applicationDidFinishLaunching , read this file and compare it with the current time. The difference will show you how long your application has been last.

+1
source share

Source: https://habr.com/ru/post/651243/


All Articles