Reputation: 2535
I want to perform an action, ten seconds after the app goes to background and in condition that the user didn't back to the app during that time.
For doing so, I am using this code:
-(void)applicationDidEnterBackground:(UIApplication *)application{
UIBackgroundTaskIdentifier __block bgTask = 0;
UIApplication *app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self checkSession];
});
}
-(void)checkSession{
sleep(10);
if([[UIApplication sharedApplication] applicationState] != UIApplicationStateActive) {
NSLog(@"Do action after 10 seconds");
}
else{
NSLog(@"Do nothing after 10 seconds");
}
}
The problem is that if the user terminate the app while it in background (By swipe it and dismiss it) , but before 10 seconds passed, the action will not be performed, and i'll see this log:
<AppDelegate: 0x15557890> ending background task 5
Any idea how to perform the action in this case?
Upvotes: 0
Views: 110
Reputation: 936
Try to use - (void)applicationWillTerminate:(UIApplication *)application
for the case that user kills the app before your action is performed
Upvotes: 1