Bypass self signed certificate with NSURLCredential while also passing in a username and password

519 views Asked by At
    NSURLCredential *credential2 = [NSURLCredential
                  credentialForTrust:challenge.protectionSpace.serverTrust
                  credentialWithUser: @"username"
                                            password: @"password"
                                         persistence:NSURLCredentialPersistenceNone];


    [challenge.sender useCredential:credential2 forAuthenticationChallenge:challenge];

I need to bypass a self signed certificate with a UIWebView while also being able to pass in a username and a password for authentication.

 NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];

and

NSURLCredential *credential = [NSURLCredential credentialWithUser: @"username" password: @"password" persistence:NSURLCredentialPersistenceNone];

are both valid alone both they fuss when I try to combine them... Any ideas?

1

There are 1 answers

0
ZeroSkittles On BEST ANSWER

Used RestKit and the code below along with an alertView for login credentials to solve this issue.

- (id)initWithURL:(NSURL*)pageURL {

    _retainedURL = pageURL;
    [_retainedURL retain];

    RKClient *client;
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    client = [RKClient clientWithBaseURL: pageURL];
    client.username=[userDefaults objectForKey:@"UserName"];
    client.password=[userDefaults objectForKey:@"Password"];
    client.authenticationType=RKRequestAuthenticationTypeHTTPBasic;
    client.disableCertificateValidation= TRUE;
    RKRequest *request=[RKRequest requestWithURL:pageURL delegate:self];
    [client configureRequest:request];
    request.authenticationType = RKRequestAuthenticationTypeHTTPBasic;
    request.disableCertificateValidation = TRUE;
    self.authenticatedRequest = request;
    [request sendAsynchronously];

    if(self = [super init]) {
        self.URL = pageURL;
        self.availableActions = SVWebViewControllerAvailableActionsOpenInSafari | SVWebViewControllerAvailableActionsMailLink | SVWebViewControllerAvailableActionsCopyLink;
    }
    return self;
}

The login alertView adds the text for username and password to userdefaults and passes the retained URL back into the initWithURL function. The login alertview is being called when a page isn't authenticated (with login credentials).