Search code examples
iphoneiosbackground-process

How to use beginBackgroundTaskWithExpirationHandler for already running task in iOS


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.


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;
    }];