0

History:

I am working on a project for which we need to support:

  • Background Upload of files using NSURLSession.
  • The server expects file to be uploaded using Content-Type: multipart/form-data
  • Previously, I was using NSURLConnection with bound pair of Streams as depicted in this Apple Sample.
  • Now, I wish to follow similar approach with NSURLSession(Background Session) by using uploadTaskWithStreamedRequest:.
  • I have written a small stand-alone iOS Sample + a PHP server to validate my concept.

Problem: Everything works if app stays in foreground, but if during upload I press the home key, the upload fails after some time with error:

Domain=NSURLErrorDomain Code=-997 "Lost connection to background transfer service"

Also a little before the upload fails the Write/Producer Stream's NSStreamEventEndEncountered is encountered.

Note: I know the work-around where I can write whole HTTP Post body to a temp file and use NSURLSession's file upload API instead. But above is more appropriate if I can make it work.

Question: Can anyone guess what could be possible reason for the upload getting failed?

Sample Code: I have uploaded the iOS Sample Code + PHP Server Code to drop box. Here is the CODE

Thanks!

TrinitronX
  • 4,959
  • 3
  • 39
  • 66
Taha Samad
  • 1,135
  • 1
  • 9
  • 22
  • Have you tried setting up the session as a "Background Session"? See [+ (NSURLSessionConfiguration *)backgroundSessionConfigurationWithIdentifier:(NSString *)identifier](https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSURLSessionConfiguration_class/#//apple_ref/occ/clm/NSURLSessionConfiguration/backgroundSessionConfigurationWithIdentifier:) – CouchDeveloper Feb 05 '15 at 18:01
  • @CouchDeveloper Yes, I did. – Taha Samad Feb 05 '15 at 18:49

1 Answers1

1

You can't upload streamed tasks using Background Configuration. I successfully upload data only in two cases:

  1. Download task with data stored in request body.
  2. Upload task from file. In that case you will not receive response body.
Skie
  • 1,942
  • 16
  • 27
  • What you wrote is true for iOS 7. But for iOS 8+ you can use data tasks and background session. – Shmidt Mar 10 '16 at 21:40
  • @Shmidt yes, but they can't be streamed data tasks (as stated in Apple docs): * Only upload tasks from a file are supported (uploading from data objects or a stream will fail after the program exits). – Skie Mar 12 '16 at 13:36
  • Sorry, I didn't noticed that it's about streams. – Shmidt Mar 12 '16 at 17:30