1
votes

Using NSUrlSession with a background configuration let's me download files even if the app gets terminated by iOS. Being curious, I tried to add an upload task and noticed that it won't continue, even not if the app is only suspended.

Apple talks about "Downloading in the background" but they don't explicitly state that uploading would not be possible.

Can somebody confirm that uploads and background session configuration don't work together?

1

1 Answers

0
votes

They DO work together.

What you need to do is the following:

Have a NSURLSessionConfiguration with background configuration

NSURLSessionConfiguration *conf = [NSURLSessionConfiguration backgroundSessionConfiguration:@"backgroundSession"];

Setup a NSURLSession (@property NSURLSession *urlSession)

Obtain the path to the file (fullPath)

Create the NSURLRequest (request)

Create a NSURLSessionTask

NSURLSessionTask*task = [self.urlSession uploadTaskWithRequest:request fromFile:fullPath];
[task resume];

And the task will run in background. You can get the status from NSURLSession delegate methods.

Cheers