tags:

views:

968

answers:

1

How can you, in the iPhone SDK, programatically access a password protected site that has been protected by .htaccess or access control.

Using NSURLRequest, you can make a GET request from a URL. But if the URL is protected how would you send the username password pair. Also, what if it's encrypted with standard MD5 encryption.

+8  A: 

Add something like this to your HTTPConnectionDelegate:

-(void)connection:(NSURLConnection *)aConnection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
        if ([challenge previousFailureCount] == 0) {
                NSURLCredential *newCredential;
                newCredential = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceNone];
                [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
        } else {
                [[challenge sender] cancelAuthenticationChallenge:challenge];
        }
}
Marc Novakowski
Thanks..Is this passed as clear text? Some webservers reject clear text passwords.
L. DPenha
The docs aren't clear, but I'd have to guess that it uses the authentication method requested by the server. If that's basic-auth, then it won't be encrypted (unless the connection is SSL). If it's digest auth, then it will use the md5 one-way hashing method.
Marc Novakowski