Edit: Misunderstood the questions. Here is a possible alternative.
- (void)applicationDidEnterBackground:(UIApplication *)application{ UIApplication* app = [UIApplication sharedApplication]; task = [app beginBackgroundTaskWithExpirationHandler:^{ [app endBackgroundTask:task]; task = UIBackgroundTaskInvalid; }]; // Start the long-running task and return immediately. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // Do the work associated with the task. NSLog(@"Started background task timeremaining = %f", [app backgroundTimeRemaining]); // save time to NSUserdefaults [[NSUserdefaults standardUserDefaults] setObject:[NSDate date] objectForKey@"startDate"]; [app endBackgroundTask:task]; task = UIBackgroundTaskInvalid; }); } - (void)applicationWillTerminate:(UIApplication *)application{ [[NSUserdefaults standardUserDefaults] setObject:[NSDate date] objectForKey@"terminateDate"]; } -(void)applicationDidFinishLaunching:(UIApplication)application{ //get start time & terminate time & then take a diffrence NSDate* temporaryDate1 = (NSDate*)[userDefaults objectForKey:@"startDate"]; NSDate* temporaryDate2 = (NSDate*)[userDefaults objectForKey:@"terminateDate"]; compare[temporaryDate1,temporaryDate2]; }
Here is a link to check how to compare NSDate instance.