NSURLSession Background File Upload using Bound Streams

走远了吗. 提交于 2019-12-12 01:59:40

问题


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!


回答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.


来源:https://stackoverflow.com/questions/28325169/nsurlsession-background-file-upload-using-bound-streams

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!