1
votes

I would like to know if I can retrieve the parial downloadede data of a failed NSURLSessionDownloadTask.

My use case is:

  1. I launch a download of a 1024MB file
  2. 512MB are downloaded
  3. The download fails because of network interruption
  4. When the download fails, the delagte's -(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error is called. But the error object does not contain the path to the 512MB file of downloaded data.

My question is: is it possible to retrieve the downloaded 512MB using the NSURLSession APIs with a background session?

Thanks,

2
Doesn't look like it. You should probably write your own version; can't be hard.Droppy
What do you mean by write your own version? Should I make a subclass of NSURLSessionDownloadTask?yostane
No, use iOS framework network classes, or AFNetworking etc., and create a class that does the downloading for you.Droppy
But I need to take advantage of the NSURLSession background mode in order to keep the downloads running when the app is in the background. As far as I know, AFNetworking do not support a background nsurlsessionyostane
Ah OK; you cannot write your own then.Droppy

2 Answers

2
votes

Apple Documentation States:

If a transfer fails, the session object provides an NSError object either to your delegate or to the task’s completion handler. In that object, the NSURLSessionDownloadTaskResumeData key in the userInfo dictionary contains a resumeData object.

Source: https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSession_class/#//apple_ref/occ/instm/NSURLSession/downloadTaskWithResumeData:

1
votes

Further to the answer already posted (and expanding on it), you can access the data already downloaded via the error object itself, as follows:

NSData* resume_data = error.userInfo[NSURLSessionDownloadTaskResumeData];