Next Page > Hide TOC

NSURLDownload Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/Foundation.framework
Availability
Available in Mac OS X v10.2 with Safari 1.0 installed.
Available in Mac OS X v10.2.7 and later.
Companion guide
Declared in
NSURLDownload.h

Overview

NSURLDownload downloads a request asynchronously and saves the data to a file. The interface for NSURLDownload is sparse, providing methods to initialize a download, set the destination path and cancel loading the request.

NSURLDownload’s delegate methods allow an object to receive informational callbacks about the asynchronous load of the URL request. Other delegate methods provide facilities that allow the delegate to customize the process of performing an asynchronous URL load.

Note that these delegate methods will be called on the thread that started the asynchronous load operation for the associated NSURLDownload object.

Tasks

Creating a Download Instance

Resuming Partial Downloads

Canceling a Download

Getting Download Properties

Setting the Destination Path

Download progress

Class Methods

canResumeDownloadDecodedWithEncodingMIMEType:

Returns whether a URL download object can resume a download that was decoded with the specified MIME type.

+ (BOOL)canResumeDownloadDecodedWithEncodingMIMEType:(NSString *)MIMEType

Parameters
MIMEType

The MIME type the caller wants to know about.

Return Value

YES if the URL download object can resume a download that was decoded with the specified MIME type, NO otherwise.

Discussion

NSURLDownload cannot resume a download that was partially decoded in the gzip format.

Availability
Declared In
NSURLDownload.h

Instance Methods

cancel

Cancels the receiver’s download and deletes the downloaded file.

- (void)cancel

Availability
Declared In
NSURLDownload.h

deletesFileUponFailure

Returns whether the receiver deletes partially downloaded files when a download stops prematurely.

- (BOOL)deletesFileUponFailure

Return Value

YES if partially downloaded files should be deleted when a download stops prematurely, NO otherwise. The default is YES.

Availability
See Also
Declared In
NSURLDownload.h

initWithRequest:delegate:

Returns an initialized URL download for a URL request and begins to download the data for the request.

- (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate

Parameters
request

The URL request to download. The request object is deep-copied as part of the initialization process. Changes made to request after this method returns do not affect the request that is used for the loading process.

delegate

The delegate for the download. This object will receive delegate messages as the download progresses. Delegate messages will be sent on the thread which calls this method. For the download to work correctly the calling thread’s run loop must be operating in the default run loop mode.

Return Value

An initialized NSURLDownload object for request.

Availability
Declared In
NSURLDownload.h

initWithResumeData:delegate:path:

Returns an initialized NSURLDownload object that will resume downloading the specified data to the specified file and begins the download.

- (id)initWithResumeData:(NSData *)resumeData delegate:(id)delegate path:(NSString *)path

Parameters
resumeData

Specifies the data to resume downloading.

delegate

The delegate for the download. This object will receive delegate messages as the download progresses. Delegate messages will be sent on the thread which calls this method. For the download to work correctly the calling thread’s run loop must be operating in the default run loop mode.

path

The location for the downloaded data.

Return Value

An initialized NSURLDownload object.

Availability
See Also
Declared In
NSURLDownload.h

request

Returns the request that initiated the receiver’s download.

- (NSURLRequest *)request

Return Value

The URL request that initiated the receiver's download.

Availability
Declared In
NSURLDownload.h

resumeData

Returns the resume data for a download that is not yet complete.

- (NSData *)resumeData

Return Value

The resume data for a download that is not yet complete. This data represents the necessary state information that an NSURLDownload object needs to resume a download. The resume data can later be used when initializing a download with initWithResumeData:delegate:path:. Returns nil if the download is not able to be resumed.

Discussion

Resume data will only be returned if the protocol of the download as well as the server support resuming. In order to later resume a download you must call setDeletesFileUponFailure: passing NO so the partially downloaded data is not deleted when the initial connection is lost or canceled.

Availability
Declared In
NSURLDownload.h

setDeletesFileUponFailure:

Specifies whether the receiver deletes the partially downloaded file when a download stops prematurely.

- (void)setDeletesFileUponFailure:(BOOL)deletesFileUponFailure

Parameters
deletesFileUponFailure

YES if partially downloaded files should be deleted when a download stops prematurely, NO otherwise. The default is YES.

Discussion

To allow the download to be resumed in case the download ends prematurely you should call this method as soon as possible after starting the download.

Availability
See Also
Declared In
NSURLDownload.h

setDestination:allowOverwrite:

Sets the destination path of the downloaded file.

- (void)setDestination:(NSString *)path allowOverwrite:(BOOL)allowOverwrite

Parameters
path

The path for the downloaded file.

allowOverwrite

YES if an existing file at path can be replaced, NO otherwise.

Discussion

If allowOverwrite is NO and a file already exists at path, a unique filename will be created for the downloaded file by appending a number to the filename. The delegate can implement download:didCreateDestination: to determine the filename used when the file is written to disk.

Special Considerations

An NSURLDownload instance ignores multiple calls to this method.

Availability
See Also
Declared In
NSURLDownload.h

Delegate Methods

download:decideDestinationWithSuggestedFilename:

The delegate receives this message when download has determined a suggested filename for the downloaded file.

- (void)download:(NSURLDownload *)download decideDestinationWithSuggestedFilename:(NSString *)filename

Parameters
download

The URL download object sending the message.

filename

The suggested filename for the download.

Discussion

The suggested filename is either derived from the last path component of the URL and the MIME type or, if the download was encoded, from the encoding. If the delegate wishes to modify the path, it should send setDestination:allowOverwrite: to download.

Special Considerations

The delegate will not receive this message if setDestination:allowOverwrite: has already been called for the download.

Availability
Declared In
NSURLDownload.h

download:didCancelAuthenticationChallenge:

Sent if an authentication challenge is canceled due to the protocol implementation encountering an error.

- (void)download:(NSURLDownload *)download didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge

Parameters
download

The URL download object sending the message.

challenge

The authentication challenge that caused the download object to cancel the download.

Discussion

If the delegate receives this message the download will fail and the delegate will receive a download:didFailWithError: message.

Availability
Declared In
NSURLDownload.h

download:didCreateDestination:

Sent when the destination file is created.

- (void)download:(NSURLDownload *)download didCreateDestination:(NSString *)path

Parameters
download

The URL download object sending the message.

path

The path to the destination file.

Availability
Declared In
NSURLDownload.h

download:didFailWithError:

Sent if the download fails or if an I/O error occurs when the file is written to disk.

- (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error

Parameters
download

The URL download object sending the message.

error

The error that caused the failure of the download.

Discussion

Any partially downloaded file will be deleted.

Special Considerations

Once the delegate receives this message, it will receive no further messages for download.

Availability
Declared In
NSURLDownload.h

download:didReceiveAuthenticationChallenge:

Sent when the URL download must authenticate a challenge in order to download the request.

- (void)download:(NSURLDownload *)download didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge

Parameters
download

The URL download object sending the message.

challenge

The URL authentication challenge that must be authenticated in order to download the request.

Discussion

This method gives the delegate the opportunity to determine the course of action taken for the challenge: provide credentials, continue without providing credentials or cancel the authentication challenge and the download.

The delegate can determine the number of previous authentication challenges by sending the message previousFailureCount to challenge.

If the previous failure count is 0 and the value returned by proposedCredential is nil, the delegate can create a new NSURLCredential object, providing a user name and password, and send a useCredential:forAuthenticationChallenge: message to [challenge sender], passing the credential and challenge as parameters. If proposedCredential is not nil, the value is a credential from the URL or the shared credential storage that can be provided to the user as feedback.

The delegate may decide to abandon further attempts at authentication at any time by sending [challenge sender] a continueWithoutCredentialForAuthenticationChallenge: or a cancelAuthenticationChallenge: message. The specific action will be implementation dependent.

If the delegate implements this method, the download will suspend until [challenge sender] is sent one of the following messages: useCredential:forAuthenticationChallenge:, continueWithoutCredentialForAuthenticationChallenge: or cancelAuthenticationChallenge:.

If the delegate does not implement this method the default implementation is used. If a valid credential for the request is provided as part of the URL, or is available from the NSURLCredentialStorage the [challenge sender] is sent a useCredential:forAuthenticationChallenge: with the credential. If the challenge has no credential or the credentials fail to authorize access, then continueWithoutCredentialForAuthenticationChallenge: is sent to [challenge sender] instead.

Availability
Declared In
NSURLDownload.h

download:didReceiveDataOfLength:

Sent as a download object receives data incrementally.

- (void)download:(NSURLDownload *)download didReceiveDataOfLength:(NSUInteger)length

Parameters
download

The URL download object sending the message.

length

The amount of data received in this increment of the download, measured in bytes.

Availability
Declared In
NSURLDownload.h

download:didReceiveResponse:

Sent when a download object has received sufficient load data to construct the NSURLResponse object for the download.

- (void)download:(NSURLDownload *)download didReceiveResponse:(NSURLResponse *)response

Parameters
download

The URL download object sending the message.

response

The URL response object received as part of the download. response is immutable and will not be modified after this method is called.

Discussion

In some rare cases, multiple responses may be received for a single download. In this case, the client should assume that each new response resets the download progress to 0 and should check the new response for the expected content length.

Availability
Declared In
NSURLDownload.h

download:shouldDecodeSourceDataOfMIMEType:

Sent when a download object determines that the downloaded file is encoded to inquire whether the file should be automatically decoded.

- (BOOL)download:(NSURLDownload *)download shouldDecodeSourceDataOfMIMEType:(NSString *)encodingType

Parameters
download

The URL download object sending the message.

encodingType

The type of encoding used by the downloaded file. The supported encoding formats are MacBinary ("application/macbinary"), Binhex ("application/mac-binhex40") and gzip ("application/gzip").

Return Value

YES to decode the file, NO otherwise.

Special Considerations

The delegate may receive this message more than once if the file has been encoded multiple times. This method is not called if the downloaded file is not encoded.

Availability
Declared In
NSURLDownload.h

download:willResumeWithResponse:fromByte:

Sent when a download object has received a response from the server after attempting to resume a download.

- (void)download:(NSURLDownload *)download willResumeWithResponse:(NSURLResponse *)response fromByte:(long long)startingByte

Parameters
download

The URL download object sending the message.

response

The URL response received from the server in response to an attempt to resume a download.

startingByte

The location of the start of the resumed data, in bytes.

Availability
Declared In
NSURLDownload.h

download:willSendRequest:redirectResponse:

Sent when the download object determines that it must change URLs in order to continue loading a request.

- (NSURLRequest *)download:(NSURLDownload *)download willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse

Parameters
download

The URL download object sending the message.

request

The proposed redirected request. The delegate should inspect the redirected request to verify that it meets its needs, and create a copy with new attributes to return to the connection if necessary.

redirectResponse

The URL response that caused the redirect. May be nil in cases where this method is not being sent as a result of involving the delegate in redirect processing.

Return Value

The actual URL request to use in light of the redirection response. The delegate may copy and modify request as necessary to change its attributes, return request unmodified, or return nil.

Discussion

If the delegate wishes to cancel the redirect, it should call the download object’s cancel method. Alternatively, the delegate method can return nil to cancel the redirect, and the download will continue to process. This has special relevance in the case where redirectResponse is not nil. In this case, any data that is loaded for the download will be sent to the delegate, and the delegate will receive a downloadDidFinish: or download:didFailWithError: message, as appropriate.

Special Considerations

The delegate can receive this message as a result of transforming a request’s URL to its canonical form, or for protocol-specific reasons, such as an HTTP redirect. The delegate implementation should be prepared to receive this message multiple times.

Availability
Declared In
NSURLDownload.h

downloadDidBegin:

Sent immediately after a download object begins a download.

- (void)downloadDidBegin:(NSURLDownload *)download

Parameters
download

The URL download object sending the message.

Availability
Declared In
NSURLDownload.h

downloadDidFinish:

Sent when a download object has completed downloading successfully and has written its results to disk.

- (void)downloadDidFinish:(NSURLDownload *)download

Parameters
download

The URL download object sending the message.

Discussion

The delegate will receive no further messages for download.

Availability
Declared In
NSURLDownload.h

Next Page > Hide TOC


© 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-05-23)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.