我希望允许带有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

最佳答案

只需使用iPhone Configuration UtilityApple Configurator在设备上添加该证书。

09-27 03:14