0

Now I need to get the user opening the APP's time,and compare the time to the last time, if the time is more than 24 hours, just to remind the user of updating App. Who knows the Objective-C code about this? Please help me .Thank U very much!

Alisa_stranger
  • 71
  • 1
  • 1
  • 7

3 Answers3

4

Create a NSDate member variable (i.e NSDate *startDate) in your AppDelegate file. Now in AppDelegate.m class use the below code.

- (void)applicationDidEnterBackground:(UIApplication *)application {
// save the last app opening time, you can save in NSuserdefaults also
startDate = [NSDate date]; 
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
NSDate *now = [NSDate date];
// Returns in seconds
NSTimeInterval timeInterval = [now timeIntervalSinceDate:startDate]

if(timeInterval > (24*3600)) {
          // remind the user of updating App here.
    }
}
vivekDas
  • 1,248
  • 8
  • 12
3

I will suggest to store the values in user defaults , so that if user exits the app even then we can have the data time details. Add the below code in app delegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

 NSDate *dateTimenow=[NSDate date];
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"dd-MM-yyyy hh:mm"];
NSString *strDateTimeNow=[NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:dateTimenow]];
NSLog(@"now date time: %@",strDateTimeNow);

NSUserDefaults *userdefaults=[NSUserDefaults standardUserDefaults];

if ([userdefaults valueForKey:@"LastAppLaunchTime"]) {
    NSString *strPreviousDateTime=[NSString stringWithFormat:@"%@",[userdefaults valueForKey:@"LastAppLaunchTime"]];
    NSLog(@"previous date time: %@",strPreviousDateTime);

    //Now you can compare current date time with previous date time
    //perform task after comparing



}


[userdefaults setValue:strDateTimeNow forKey:@"LastAppLaunchTime"];// saving recent date time details for next time

you can also do this while going in background and coming to foreground, so if app is suspended in background for more than 24 hours then also you can perform your task. I hope this helps.

NewStackUser
  • 657
  • 5
  • 17
0

You can use NSDate:

NSDate* now = [NSDate date];

Save this value as lastTimeOpened and compare during next launch:

if ([now timeIntervalSinceDate:lastTimeOpened] > someTime)
izik461
  • 1,131
  • 12
  • 32