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

后端 未结 1 1137
轻奢々
轻奢々 2021-02-02 04:48

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 req

相关标签:
1条回答
  • 2021-02-02 05:04

    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];
            }
    }
    
    0 讨论(0)
提交回复
热议问题