编辑:误解了问题。这是一个可能的选择。
- (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] [email protected]"startDate"];
[app endBackgroundTask:task];
task = UIBackgroundTaskInvalid;
});
}
- (void)applicationWillTerminate:(UIApplication *)application{
[[NSUserdefaults standardUserDefaults] setObject:[NSDate date] [email protected]"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];
}
这是一个link来检查如何比较NSDate实例。
他希望能够设置应用程序在后台运行的时候,没怎么检查它的时间长短的背景。 – JonahGabriel
@ Jonah.at.GoDaddy检查我的更新答案 –
看起来不错,但这并保持应用从在后台,以及在10分钟后,被关停?或者这不再发生? – user2551731