Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

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.

like image 844
L. DPenha Avatar asked Dec 13 '25 19:12

L. DPenha


1 Answers

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];
        }
}
like image 73
Marc Novakowski Avatar answered Dec 16 '25 11:12

Marc Novakowski