根据你的意见,你得到一个无效的证书错误,并且你正在使用NSURLConnection你的解决方案是实现这些委托。看看这是否解决了这个问题。
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge]; }
注意,你基本上是忽略SSL错误,并允许连接继续进行。所以,如果你打开了黑客应用程序。为了进行测试,您可以使用此功能,但请确保在生产环境中安装适当的证书。
但是,如果你想正确处理这个问题,并在证书不正确的情况下警告用户,你可以这样做。
OSStatus err = noErr;
BOOL trusted = NO;
NSURLProtectionSpace * protectionSpace = challenge.protectionSpace;
SecTrustRef serverTrustRef = protectionSpace.serverTrust;
SecTrustResultType trustResult;
//check if the server trust that we got from the server can be trusted by default
err = SecTrustEvaluate(serverTrustRef, &trustResult);
trusted = (err == noErr) && ((trustResult == kSecTrustResultProceed) || (trustResult == kSecTrustResultUnspecified));
if (trusted) //if the site is trusted then continue with that trust
{
[challenge.sender useCredential:[NSURLCredential credentialForTrust:protectionSpace.serverTrust]
forAuthenticationChallenge:challenge];
}
else //if not then warn the user about this and let the user make a decision
{
//warn the user about the cert problem using a dialog with options to continue or ignore.
//Continue alert action call : [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]
forAuthenticationChallenge:challenge];
//Dont continue action: [challenge.sender continueWithoutCredentialForAuthenticationChallenge:_challenge];
//OR call: [sender cancelAuthenticationChallenge:challenge];
}
}
可以调用使用该设备的浏览器相同的网址是什么? –
它给我一个错误:无法验证服务器身份 –
你打开此网址在网络视图或使用它来使用NSURLSession或NSURLConnection类进行网络API调用?如果可以的话,你也介意分享完整的网址吗?看起来您需要处理身份验证挑战,因为您看到的错误是由于身份不明的证书。在这种情况下,您必须实现在连接期间调用的某些委托方法,并相应地处理它们。 –