NSURLConnectionDelegate Protocol Reference
| Conforms to | |
| Framework | /System/Library/Frameworks/Foundation.framework |
| Availability | Available in OS X v10.7 and later. |
| Companion guide | |
| Declared in | NSURLConnection.h |
Overview
The NSURLConnectionDelegate protocol defines the optional methods implemented by delegates of NSURLConnection objects.
Delegates should implement the connection:willSendRequestForAuthenticationChallenge: method, which is the preferred mechanism for responding to authentication challenges. (See NSURLAuthenticationChallenge Class Reference for more information on authentication challenges.) If connection:willSendRequestForAuthenticationChallenge: is not implemented, the older, deprecated methods connection:canAuthenticateAgainstProtectionSpace:, connection:didReceiveAuthenticationChallenge:, and connection:didCancelAuthenticationChallenge: are called instead.
The connection:didFailWithError: method is called at most once if an error occurs during the loading of a resource. The connectionShouldUseCredentialStorage: method is called once, just before the loading of a resource begins.
Tasks
Connection Authentication
-
– connection:willSendRequestForAuthenticationChallenge: -
– connection:canAuthenticateAgainstProtectionSpace: -
– connection:didCancelAuthenticationChallenge: -
– connection:didReceiveAuthenticationChallenge: -
– connectionShouldUseCredentialStorage:
Connection Completion
MethodGroup
-
– connection:willCacheResponse:required method -
– connection:didReceiveResponse:required method -
– connection:didReceiveData:required method -
– connection:needNewBodyStream -
– connection:willSendRequest:redirectResponse:required method -
– connectionDidFinishLoading:required method -
– connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:required method Available in OS X v10.6 through OS X v10.7
Instance Methods
connection:canAuthenticateAgainstProtectionSpace:
Sent to determine whether the delegate is able to respond to a protection space’s form of authentication.
Parameters
- connection
The connection sending the message.
- protectionSpace
The protection space that generates an authentication challenge.
Return Value
YES if the delegate if able to respond to a protection space’s form of authentication, otherwise NO.
Discussion
This method is called before connection:didReceiveAuthenticationChallenge:, allowing the delegate to inspect a protection space before attempting to authenticate against it. By returning YES, the delegate indicates that it can handle the form of authentication, which it does in the subsequent call to connection:didReceiveAuthenticationChallenge:. If the delegate returns NO, the system attempts to use the user’s keychain to authenticate. If your delegate does not implement this method and the protection space uses client certificate authentication or server trust authentication, the system behaves as if you returned NO. The system behaves as if you returned YES for all other authentication methods.
Availability
- Available in OS X v10.6 and later.
- Available as part of an informal protocol prior to OS X v10.7.
Declared In
NSURLConnection.hconnection:didCancelAuthenticationChallenge:
Sent when a connection cancels an authentication challenge.
Parameters
- connection
The connection sending the message.
- challenge
The challenge that was canceled.
Availability
- Available in OS X v10.2 and later.
- Available as part of an informal protocol prior to OS X v10.7.
Declared In
NSURLConnection.hconnection:didFailWithError:
Sent when a connection fails to load its request successfully.
Parameters
- connection
The connection sending the message.
- error
An error object containing details of why the connection failed to load the request successfully.
Discussion
Once the delegate receives this message, it will receive no further messages for connection.
Availability
- Available in OS X v10.2 and later.
- Available as part of an informal protocol prior to OS X v10.7.
Declared In
NSURLConnection.hconnection:didReceiveAuthenticationChallenge:
Sent when a connection must authenticate a challenge in order to download its request.
Parameters
- connection
The connection sending the message.
- challenge
The challenge that connection must authenticate in order to download its 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 information specific to the type of credential, 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 is 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
- Available in OS X v10.2 and later.
- Available as part of an informal protocol prior to OS X v10.7.
See Also
Declared In
NSURLConnection.hconnection:didReceiveData:
Sent as a connection loads data incrementally. (required)
Parameters
- connection
The connection sending the message.
- data
The newly available data. The delegate should concatenate the contents of each data object delivered to build up the complete data for a URL load.
Discussion
This method provides the only way for an asynchronous delegate to retrieve the loaded data. It is the responsibility of the delegate to retain or copy this data as it is delivered.
Availability
- Available in OS X v10.2 with Safari 1.0 installed.
- Available in OS X v10.2.7 and later.
Declared In
NSURLConnection.hconnection:didReceiveResponse:
Sent when the connection has received sufficient data to construct the URL response for its request. (required)
Parameters
- connection
The connection sending the message.
- response
The URL response for the connection's request. This object is immutable and will not be modified by the URL loading system once it is presented to the delegate.
Discussion
In rare cases, for example in the case of an HTTP load where the content type of the load data is multipart/x-mixed-replace, the delegate will receive more than one connection:didReceiveResponse: message. In the event this occurs, delegates should discard all data previously delivered by connection:didReceiveData:, and should be prepared to handle the, potentially different, MIME type reported by the newly reported URL response.
The only case where this message is not sent to the delegate is when the protocol implementation encounters an error before a response could be created.
Availability
- Available in OS X v10.2 with Safari 1.0 installed.
- Available in OS X v10.2.7 and later.
Declared In
NSURLConnection.hconnection:needNewBodyStream
Called when an NSURLConnection needs to retransmit a request that has a body stream to provide a new, unopened stream.
Parameters
- connection
The NSURLConnection that is requesting a new body stream.
Return Value
This delegate method should return a new, unopened stream that provides the body contents for the request.
If this delegate method returns NULL, the connection fails.
Discussion
On OS X, if this method is not implemented, body stream data is spooled to disk in case retransmission is required. This spooling may not be desirable for large data sets.
By implementing this delegate method, the client opts out of automatic spooling, and must provide a new, unopened stream for each retransmission.
connection:willCacheResponse:
Sent before the connection stores a cached response in the cache, to give the delegate an opportunity to alter it. (required)
Parameters
- connection
The connection sending the message.
- cachedResponse
The proposed cached response to store in the cache.
Return Value
The actual cached response to store in the cache. The delegate may return cachedResponse unmodified, return a modified cached response, or return nil if no cached response should be stored for the connection.
Availability
- Available in OS X v10.2 with Safari 1.0 installed.
- Available in OS X v10.2.7 and later.
Declared In
NSURLConnection.hconnection:willSendRequest:redirectResponse:
Sent when the connection determines that it must change URLs in order to continue loading a request. (required)
Parameters
- connection
The connection 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 return request unmodified to allow the redirect, return a new request, or return nil to reject the redirect and continue processing the connection.
Discussion
If the delegate wishes to cancel the redirect, it should call the connection object’s cancel method. Alternatively, the delegate method can return nil to cancel the redirect, and the connection 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 connection will be sent to the delegate, and the delegate will receive a connectionDidFinishLoading or connection:didFailLoadingWithError: message, as appropriate.
The delegate can receive this message as a result of modifying a request before it is sent, for example to transform the request’s URL to its canonical form. To detect this case, examine redirectResponse; if it is nil, the message was not sent due to a redirect.
The delegate should be prepared to receive this message multiple times.
Availability
- Available in OS X v10.2 with Safari 1.0 installed.
- Available in OS X v10.2.7 and later.
Declared In
NSURLConnection.hconnection:willSendRequestForAuthenticationChallenge:
Tells the delegate that the connection will send a request for an authentication challenge.
Parameters
- connection
The connection sending the message.
- challenge
The authentication challenge for which a request is being sent.
Discussion
This method allows the delegate to make an informed decision about connection authentication at once. If the delegate implements this method, it has no need to implement connection:canAuthenticateAgainstProtectionSpace:, connection:didReceiveAuthenticationChallenge:, connectionShouldUseCredentialStorage:. In fact, these other methods are not invoked.
In this method,you must invoke one of the challenge-responder methods (NSURLAuthenticationChallengeSender protocol):
You might also want to analyze challenge for the authentication scheme and the proposed credential before calling a NSURLAuthenticationChallengeSender method. You should never assume that a proposed credential is present. You can either create your own credential and respond with that, or you can send the proposed credential back. (Because this object is immutable, if you want to change it you must copy it and then modify the copy.)
Availability
- Available in OS X v10.7 and later.
Declared In
NSURLConnection.hconnectionDidFinishLoading:
Sent when a connection has finished loading successfully. (required)
Parameters
- connection
The connection sending the message.
Discussion
The delegate will receive no further messages for connection.
Availability
- Available in OS X v10.2 with Safari 1.0 installed.
- Available in OS X v10.2.7 and later.
Declared In
NSURLConnection.hconnectionShouldUseCredentialStorage:
Sent to determine whether the URL loader should consult the credential storage for authenticating the connection.
Parameters
- connection
The connection sending the message.
Discussion
This method is called before any attempt to authenticate is made. By returning NO, the delegate tells the connection not to consult the credential storage and makes itself responsible for providing credentials for any authentication challenges. Not implementing this method is the same as returning YES. The delegate is free to consult the credential storage itself when it receives a connection:didReceiveAuthenticationChallenge: message.
Availability
- Available in OS X v10.6 and later.
- Available as part of an informal protocol prior to OS X v10.7.
Declared In
NSURLConnection.h© 2012 Apple Inc. All Rights Reserved. (Last updated: 2012-05-14)