Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 15. Networking > Handling Authentication Challenges

Handling Authentication Challenges

Some websites are protected with usernames and passwords. NSURLConnection lets you access these sites by responding to authentication challenges. Prior to iOS 5, you may have handled credentials using delegate callbacks like this:

- (void)connection:(NSURLConnection *)connection
    didReceiveAuthenticationChallenge:
    (NSURLAuthenticationChallenge *)challenge
{
    NSURLProtectionSpace *protectionSpace =
        [[NSURLProtectionSpace alloc]
            initWithHost:primaryHost port:0
            protocol:@"http" realm:nil
            authenticationMethod:nil];
    NSURLCredential *credential =
        [[NSURLCredentialStorage sharedCredentialStorage]
            defaultCredentialForProtectionSpace:protectionSpace];

    [[challenge sender] useCredential:credential
        forAuthenticationChallenge:challenge];
}

- (void)connection:(NSURLConnection *)connection
    didCancelAuthenticationChallenge:
    (NSURLAuthenticationChallenge *)challenge
{
    NSLog(@"Challenge cancelled");
}


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial