i want to terminate the app after some minutes while it run in background
i want to terminate the app after some minutes while it runs in background.
steps like these :
press the home button then app run in background. 2minutes later the app terminates automaticly.
i find a method as below:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UIApplication* app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = 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.
//============================
//开发者_如何转开发 here i want to add a timer
//=============================
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}
as you see, i want to add a timer in that place.
but the timer didn't work in background. i also tried the performSelector:afterdelay:
how can i do this?
After 10 minutes, All app terminates.
If you have to exit app only after 2 mins,
set timer in (applicationWillResignActive)
I suppose your app is doing some kind of work while in background - otherwise there would be no need to run it in the background in the first place.
I further suppose you have some kind of runloop going, since you'd like to terminate after a certain amount of time and not after some task has been completed.
So I'd suggest logging the system time at the moment the app goes into background and then in your runloop check for the current system time once per iteration. If it is greater than the initial time + 2 minutes, terminate background activity.
精彩评论