Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 943
joshim5 Avatar asked Jan 19 '11 14:01

joshim5


People also ask

Can you see when an app was last used on iPhone?

Double-tap the "Home" button to open the list of most recently used applications on your iPhone.


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
like image 162
Jilouc Avatar answered Nov 01 '22 14:11

Jilouc


In your application delegates - (void)applicationDidEnterBackground:(UIApplication *)application method, just write the current time to a file. Then, in either applicationWillEnterForeground: or applicationDidFinishLaunching, read this file and compare with the current time. The difference will tell you how long since your application was last foremost.

like image 26
Adam Wright Avatar answered Nov 01 '22 13:11

Adam Wright