1

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,

yostane
  • 383
  • 1
  • 4
  • 19
  • Doesn't look like it. You should probably write your own version; can't be hard. – Droppy Oct 15 '14 at 09:54
  • What do you mean by write your own version? Should I make a subclass of NSURLSessionDownloadTask? – yostane Oct 15 '14 at 12:36
  • No, use iOS framework network classes, or AFNetworking etc., and create a class that does the downloading for you. – Droppy Oct 15 '14 at 12:42
  • 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 nsurlsession – yostane Oct 15 '14 at 13:04
  • 1
    Ah OK; you cannot write your own then. – Droppy Oct 15 '14 at 13:10
  • In conclusion, the answer of my post is no I cannot. Thanks for the replies ^^. – yostane Oct 15 '14 at 13:12

2 Answers2

2

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

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];
Ash
  • 2,021
  • 2
  • 26
  • 59