问题
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 usinguploadTaskWithStreamedRequest
:. - 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!
回答1:
You can't upload streamed tasks using Background Configuration. I successfully upload data only in two cases:
- Download task with data stored in request body.
- Upload task from file. In that case you will not receive response body.
来源:https://stackoverflow.com/questions/28325169/nsurlsession-background-file-upload-using-bound-streams