NSURLConnectionDelegate Protocol Reference
| Conforms to | |
| Framework | /System/Library/Frameworks/Foundation.framework |
| Availability | Available in iOS 5.0 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:needNewBodyStream -
– connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:required method Available in iOS 3.0 through iOS 4.3 -
– connection:didReceiveData:required method Available in iOS 2.0 through iOS 4.3 -
– connection:didReceiveResponse:required method Available in iOS 2.0 through iOS 4.3 -
– connection:willCacheResponse:required method Available in iOS 2.0 through iOS 4.3 -
– connection:willSendRequest:redirectResponse:required method Available in iOS 2.0 through iOS 4.3 -
– connectionDidFinishLoading:required method Available in iOS 2.0 through iOS 4.3
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 iOS 3.0 and later.
- Available as part of an informal protocol prior to iOS 5.0.
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 iOS 2.0 and later.
- Available as part of an informal protocol prior to iOS 5.0.
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 iOS 2.0 and later.
- Available as part of an informal protocol prior to iOS 5.0.
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 iOS 2.0 and later.
- Available as part of an informal protocol prior to iOS 5.0.
See Also
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: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 iOS 5.0 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 iOS 3.0 and later.
- Available as part of an informal protocol prior to iOS 5.0.
Declared In
NSURLConnection.h© 2012 Apple Inc. All Rights Reserved. (Last updated: 2012-05-14)