使用NSURLConnection处理自签名证书

时间:2014-01-17 06:09:47

标签: objective-c ssl nsurlconnection

如果主机位于受信任列表中,我希望允许带有NSURLConnection的自签名证书。

我看到很多人都在做这样的事情:

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
    if ([protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
        if (allowSelfSignedCertForThisHost) {
            NSLog(@"Allowing self signed!");
            return YES;
        }
    }
    return NO;
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
        if ([trustedHosts containsObject:challenge.protectionSpace.host]) {
            [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
        }
    }
    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}

但是,我想知道为什么你会在useCredential:forAuthenticationChallenge之后调用continueWithoutCredentialForAuthenticationChallenge

1 个答案:

答案 0 :(得分:1)

只需在iPhone Configuration UtilityApple Configurator

的设备上添加该证书即可