I want to save all the data used in app in server (using API call) when user touch home button. I set the observer as
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveData) name:UIApplicationDidEnterBackgroundNotification object:nil];
the saveData
method is used to send the data to server. The api call is not worked. Any way to call API when user close the app.
I've started the background task as
- (void)applicationDidEnterBackground:(UIApplication *)application
{
bgTask = [application beginBackgroundTaskWithName:@"MyTask" expirationHandler:^{
[self saveData];
// Clean up any unfinished task business by marking where you
// stopped or ending the task outright.
}];
// 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, preferably in chunks.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}
And then added the below line inside the saveData
method
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;