iOS Developer Library

Developer

Foundation Framework Reference NSURLConnectionDownloadDelegate Protocol Reference

Options
Deployment Target:

On This Page
Language:

NSURLConnectionDownloadDelegate

The NSURLConnectionDownloadDelegate protocol describes methods that should be implemented by the delegate of instances of NSURLConnection created using Newsstand Kit’s downloadWithDelegate: method. The methods in this protocol provide progress information about the download of a URL asset and, when downloading concludes, provide a file URL where the downloaded file can be accessed.

In addition to the methods described in this protocol, an NSURLConnection delegate should also implement the methods described in the NSURLConnectionDelegate protocol.

Inheritance


Not Applicable

Import Statement


Swift

import Foundation

Objective-C

@import Foundation;

Availability


Available in iOS 5.0 and later.
  • Sent to the delegate to deliver progress information for a download of a URL asset to a destination file.

    Declaration

    Swift

    optional func connection(_ connection: NSURLConnection, didWriteData bytesWritten: Int64, totalBytesWritten totalBytesWritten: Int64, expectedTotalBytes expectedTotalBytes: Int64)

    Objective-C

    - (void)connection:(NSURLConnection *)connection didWriteData:(long long)bytesWritten totalBytesWritten:(long long)totalBytesWritten expectedTotalBytes:(long long)expectedTotalBytes

    Parameters

    connection

    The URL connection object downloading the asset.

    bytesWritten

    The number of bytes written since the last call of this method.

    totalBytesWritten

    The total number of bytes of the downloading asset that have been written to the file.

    expectedTotalBytes

    The total number of bytes of the URL asset once it is completely downloaded and written to a file. This parameter can be zero if the total number of bytes is not known.

    Discussion

    This method is invoked repeatedly during the download of a URL asset to the destination file. The delegate typically uses the values of the three “bytes” parameters to update a progress indicator in the application’s user interface.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 5.0 and later.

  • Sent to the delegate when an URL connection resumes downloading a URL asset that was earlier suspended.

    Declaration

    Swift

    optional func connectionDidResumeDownloading(_ connection: NSURLConnection, totalBytesWritten totalBytesWritten: Int64, expectedTotalBytes expectedTotalBytes: Int64)

    Objective-C

    - (void)connectionDidResumeDownloading:(NSURLConnection *)connection totalBytesWritten:(long long)totalBytesWritten expectedTotalBytes:(long long)expectedTotalBytes

    Parameters

    connection

    The URL connection object downloading the asset.

    totalBytesWritten

    The total number of bytes of the downloading asset that have been written to the destination file.

    expectedTotalBytes

    The total number of bytes of the URL asset once it is completely downloaded and written to a file.

    Discussion

    This method is invoked once a suspended download of a URL asset resumes downloading. In response, the delegate can display a progress indicator, setting the initial value of the indicator to where it was when downloading was suspended. After the URL-connection object sends this message, it sends one or more connection:didWriteData:totalBytesWritten:expectedTotalBytes: to the delegate until the download concludes.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 5.0 and later.

  • Sent to the delegate when the URL connection has successfully downloaded the URL asset to a destination file.

    Declaration

    Swift

    func connectionDidFinishDownloading(_ connection: NSURLConnection, destinationURL destinationURL: NSURL)

    Objective-C

    - (void)connectionDidFinishDownloading:(NSURLConnection *)connection destinationURL:(NSURL *)destinationURL

    Parameters

    connection

    The URL connection object that downloaded the asset.

    destinationURL

    A file URL specifying a destination in the file system. For iOS applications, this is a location in the application sandbox.

    Discussion

    This method will be called once after a successful download. The file downloaded to destinationURL is guaranteed to exist there only for the duration of this method implementation; the delegate should copy or move the file to a more persistent and appropriate location.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 5.0 and later.