NSURLSession Background File Upload using Bound Streams
Asked Answered
B

1

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!

Breban answered 4/2, 2015 at 15:31 Comment(2)
Have you tried setting up the session as a "Background Session"? See + (NSURLSessionConfiguration *)backgroundSessionConfigurationWithIdentifier:(NSString *)identifierSavitt
@Savitt Yes, I did.Breban
R
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.
Rodroda answered 10/2, 2015 at 16:39 Comment(3)
What you wrote is true for iOS 7. But for iOS 8+ you can use data tasks and background session.Barbed
@Barbed 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).Rodroda
Sorry, I didn't noticed that it's about streams.Barbed

© 2022 - 2024 — McMap. All rights reserved.