1

URLSessionDownload Task works well for video download but some Authenticated Vimeo URLs for HQ videos(large data) generates following error with no resume data.

So how do I resume download ???

I am getting error while downloading video task fails (different logs of error are):

  1. localized error: Optional(\"The operation couldn’t be completed. Protocol error\")

  2. error debug description: Optional(Error Domain=NSPOSIXErrorDomain Code=100 \"Protocol error\" UserInfo={_kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=100, NSErrorPeerAddressKey=<100201bb 97650a6d 00000000 00000000>})

  3. error unsafelyUnwrapped :Error Domain=NSPOSIXErrorDomain Code=100 \"Protocol error\" UserInfo={_kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=100, NSErrorPeerAddressKey=<100201bb 97650a6d 00000000 00000000>}"

//Code

var downloadTask: URLSessionDownloadTask?
var SessionRequest : URLRequest?

let backgroundSessionConfiguration = URLSessionConfiguration.background(withIdentifier: "\(Bundle.main.bundleIdentifier!).background")

let url = URL(string: urlString)!
SessionRequest = URLRequest(url: url)
SessionRequest?.httpMethod = "GET"
downloadTask = backgroundSession.downloadTask(with: SessionRequest!)
downloadTask?.resume()

//Delegate called sometimes when resulting into error with no RESUME DATA

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {


        if error != nil {
            debugPrint("Task completed: \(String(describing: task)),localized error: \(String(describing: error?.localizedDescription)) error debug desc: \(String(describing: error.debugDescription)) error unsafelyUnwrapped :\(error.unsafelyUnwrapped)")

        let err = error! as NSError
        let data = (err.userInfo)[NSURLSessionDownloadTaskResumeData]
         //Above data is always nil so cannot resume
        }
}
Rakshit Korat
  • 1,159
  • 10
  • 23
  • 1
    My suspicion is that the server is sending back a response that is malformed in some way, and the NSURL* stack is barfing. With that said, does the session have a delegate? Are you setting any headers on the NSURLRequest object? In your session configuration? – dgatwood Jun 04 '17 at 04:58
  • yes i am setting header as when i log response after fail, response contains header set This are headers set(I get same error if i don't set this header): SessionRequest?.setValue("video/mp4", forHTTPHeaderField: "Content-Type") SessionRequest?.setValue("bytes", forHTTPHeaderField: "Accept-Ranges") – Rakshit Korat Jun 05 '17 at 05:48
  • As far as I know, Accept-Ranges is not a valid header in a request. It is exclusively a server response field. Remove that header. – dgatwood Jun 05 '17 at 17:14
  • Still getting same error – Rakshit Korat Jun 09 '17 at 05:36
  • Add a complete set of response headers to your question. – dgatwood Jun 12 '17 at 01:21
  • Now , I am not setting any Header values but still i get same error, while i get redirect count as 1 in metrics delegate , with metrics transaction array having 3 values in which last transaction with all null values resulting in this error, i tried to post whole log here but it is exceeding word count, So how can i give you whole log ? – Rakshit Korat Jun 12 '17 at 13:13
  • Just `curl -I ` and post the results as a code listing in your question above (replacing anything that you consider confidential with *****). It shouldn't be *that* long. – dgatwood Jun 12 '17 at 18:02
  • [1] 7355 [2] 7356 [2]+ Done profile_id=119 Raxits-MacBook-Pro:~ rakshit$ HTTP/1.1 301 Moved Permanently Server: Varnish Retry-After: 0 Location: "My url" Content-Length: 0 Accept-Ranges: bytes Date: Tue, 13 Jun 2017 11:52:12 GMT Via: 1.1 varnish Connection: close X-Served-By: cache-hhn1549-HHN X-Cache: HIT X-Cache-Hits: 0 X-Timer: S1497354733.580055,VS0,VE0 – Rakshit Korat Jun 13 '17 at 11:53
  • Url works on browser, but it redirects to load video – Rakshit Korat Jun 13 '17 at 11:55
  • Please refer to https://forums.developer.apple.com/message/236220#236220 – Rakshit Korat Jun 14 '17 at 13:33
  • @RakshitKorat Did you ever find a solution to this? Struggling with the same issue. – Will Bagley Oct 10 '18 at 00:04
  • @RakshitKorat did you find a solution? – animaonline Nov 27 '18 at 09:52
  • @animaonline No, rather did it by using alamofire – Rakshit Korat Dec 20 '18 at 06:07

0 Answers0