25.12.2013 Views

URL Loading System Programming Guide - Apple Developer

URL Loading System Programming Guide - Apple Developer

URL Loading System Programming Guide - Apple Developer

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Authentication Challenges and TLS Chain Validation<br />

Responding to an Authentication Challenge<br />

Canceling the Connection<br />

The delegate may also choose to cancel the authentication challenge.<br />

●<br />

●<br />

For NS<strong>URL</strong>Session, pass NS<strong>URL</strong>SessionAuthChallengeCancelAuthenticationChallenge to the<br />

provided completion handler block.<br />

For NS<strong>URL</strong>Connection or NS<strong>URL</strong>Download, call cancelAuthenticationChallenge: on [challenge<br />

sender]. The delegate receives a connection:didCancelAuthenticationChallenge: message,<br />

providing the opportunity to give the user feedback.<br />

An Authentication Example<br />

The implementation shown in Listing 6-1 responds to the challenge by creating an NS<strong>URL</strong>Credential instance<br />

with a user name and password supplied by the application’s preferences. If the authentication has failed<br />

previously, it cancels the authentication challenge and informs the user.<br />

Listing 6-1<br />

An example of using the connection:didReceiveAuthenticationChallenge: delegate method<br />

-(void)connection:(NS<strong>URL</strong>Connection *)connection<br />

didReceiveAuthenticationChallenge:(NS<strong>URL</strong>AuthenticationChallenge *)challenge<br />

{<br />

if ([challenge previousFailureCount] == 0) {<br />

NS<strong>URL</strong>Credential *newCredential;<br />

newCredential = [NS<strong>URL</strong>Credential credentialWithUser:[self preferencesName]<br />

password:[self preferencesPassword]<br />

persistence:NS<strong>URL</strong>CredentialPersistenceNone];<br />

[[challenge sender] useCredential:newCredential<br />

forAuthenticationChallenge:challenge];<br />

} else {<br />

[[challenge sender] cancelAuthenticationChallenge:challenge];<br />

// inform the user that the user name and password<br />

// in the preferences are incorrect<br />

[self showPreferencesCredentialsAreIncorrectPanel:self];<br />

}<br />

}<br />

2013-10-22 | Copyright © 2003, 2013 <strong>Apple</strong> Inc. All Rights Reserved.<br />

56

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!