L. DPenha
L. DPenha

Reputation: 519

In iOS, how would you programmatically pass a username / password to a secure site

How can you, in iOS, programmatically 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.

Upvotes: 3

Views: 2033

Answers (1)

Marc Novakowski
Marc Novakowski

Reputation: 45398

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];
        }
}

Upvotes: 8

Related Questions