NSURLSessionDownloadDelegate Protocol Reference

Conforms to
Framework
/System/Library/Frameworks/Foundation.framework
Availability
Available in iOS 7.0 and later.
Declared in
NSURLSession.h

Overview

The NSURLSessionDownloadDelegate protocol defines delegate methods that you should implement when using NSURLSession download tasks. In addition to these methods, be sure to implement the methods in the NSURLSessionTaskDelegate and NSURLSessionDelegate protocols to handle events common to all task types and session-level events, respectively.

Tasks

Delegate Methods

Instance Methods

URLSession:downloadTask:didFinishDownloadingToURL:

Tells the delegate that a download task has finished downloading. (required)

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
Parameters
session

The session containing the download task that finished.

downloadTask

The download task that finished.

location

A file URL for the temporary file. Because the file is temporary, you must either open the file for reading or move it to a permanent location in your app’s sandbox container directory before returning from this delegate method.

If you choose to open the file for reading, you should do the actual reading in another thread to avoid blocking the delegate queue.

Availability
  • Available in iOS 7.0 and later.
Declared In
NSURLSession.h

URLSession:downloadTask:didResumeAtOffset:expectedTotalBytes:

Tells the delegate that the download task has resumed downloading. (required)

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes
Parameters
session

The session containing the download task that finished.

downloadTask

The download task that resumed. See explanation in the discussion.

fileOffset

If the file's cache policy or last modified date prevents reuse of the existing content, then this value is zero. Otherwise, this value is an integer representing the number of bytes on disk that do not need to be retrieved again.

Note: In some situations, it may be possible for the transfer to resume earlier in the file than where the previous transfer ended.

expectedTotalBytes

The expected length of the file, as provided by the Content-Length header. If this header was not provided, the value is NSURLSessionTransferSizeUnknown.

Discussion

If a resumable download task is canceled or fails, you can request a resumeData object that provides enough information to restart the download in the future. Later, you can call downloadTaskWithResumeData: or downloadTaskWithResumeData:completionHandler: with that data.

When you call those methods, you get a new download task. As soon as you resume that task, the session calls its delegate’s URLSession:downloadTask:didResumeAtOffset:expectedTotalBytes: method with that new task to indicate that the download is resumed.

Availability
  • Available in iOS 7.0 and later.
Declared In
NSURLSession.h

URLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:

Periodically informs the delegate about the download’s progress. (required)

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
Parameters
session

The session containing the download task.

downloadTask

The download task.

bytesWritten

The number of bytes transferred since the last time this delegate method was called.

totalBytesWritten

The total number of bytes transferred so far.

totalBytesExpectedToWrite

The expected length of the file, as provided by the Content-Length header. If this header was not provided, the value is NSURLSessionTransferSizeUnknown.

Availability
  • Available in iOS 7.0 and later.
Declared In
NSURLSession.h