How to use beginBackgroundTaskWithExpirationHandler for already running task in iOS

background-process, ios, iphone

Solution

I used below code for background task handler:

__block UIBackgroundTaskIdentifier backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{

    NSLog(@"Background Time:%f",[[UIApplication sharedApplication] backgroundTimeRemaining]);

    [[UIApplication sharedApplication] endBackgroundTask:backgroundTaskIdentifier];

    backgroundTaskIdentifier = UIBackgroundTaskInvalid;
}];

Problem

In my app, I am uploading images to server from iPhone. While sync if user press home button, App will close. I want app must be running in background till sync finish. My question is: How can I add currently running task with "`beginBackgroundTaskWithExpirationHandler`"? Please share your ideas.

Original source