iOS Developer Library

Developer

Foundation Framework Reference NSURLSessionDownloadDelegate Protocol Reference

Options
Deployment Target:

On This Page
Language:

NSURLSessionDownloadDelegate

Inheritance


Not Applicable

Conforms To


Import Statement


Swift

import Foundation

Objective-C

@import Foundation;

Availability


Available in iOS 7.0 and later.

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.

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

    Declaration

    Swift

    optional func URLSession(_ session: NSURLSession, downloadTask downloadTask: NSURLSessionDownloadTask, didResumeAtOffset fileOffset: Int64, expectedTotalBytes expectedTotalBytes: Int64)

    Objective-C

    - (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.

    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.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 7.0 and later.

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

    Declaration

    Swift

    optional func URLSession(_ session: NSURLSession, downloadTask downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten totalBytesWritten: Int64, totalBytesExpectedToWrite totalBytesExpectedToWrite: Int64)

    Objective-C

    - (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.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 7.0 and later.

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

    Declaration

    Swift

    func URLSession(_ session: NSURLSession, downloadTask downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL)

    Objective-C

    - (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.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 7.0 and later.