1

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?

Krumelur
  • 32,180
  • 27
  • 124
  • 263

1 Answers1

0

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

Lubakis
  • 141
  • 1
  • 8
  • Yeah, that's what I do and it works great on the Simulator but not on the device. iOS8 Simulator does not suspend threads if an app gets backgrounded. If you have a working example, please let me know. – Krumelur Jul 09 '15 at 20:19
  • Have you enabled background modes in your application? – Lubakis Jul 10 '15 at 06:07
  • What specific background mode would you like me to enable? There isn't one for background transfer. And as stated: downloads work fine, upload's don't. – Krumelur Jul 10 '15 at 06:54
  • You were actually right. It does work in the background but it is very, very flaky and undocumented. By the way: it is *not* necessary to enable any background modes. – Krumelur Jul 13 '15 at 15:43
  • @Krumelur Did you figure out how to manage the uploads in the backgrounds? the documentation is still weak. – Mayur Tolani Nov 30 '18 at 02:56