NSURLConnection HTTP加载失败(kCFStreamErrorDomainSSL,-9813)
当我使用http://domain.com
时,我的应用工作正常。但今天,当我将http://
更改为https://
时,我面临的问题是:NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)
。我也对.plist
进行了更改。这里是我的.plist
代码:NSURLConnection HTTP加载失败(kCFStreamErrorDomainSSL,-9813)
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>dev.domainName.in</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
</dict>
</dict>
</dict>
不过还是我面对这个问题。请帮助我。我错过了什么?
从iOS 9开始,Apple已经执行了ATS。 Here是有关这方面的文档。试着放松限制,看看它是否有效。
它适用于运行iOS 9之前的操作系统版本的设备吗?
我已阅读本文档并使用它们提供的代码。但它仍然给我一个错误。 –
请尝试使用本网站检查您的证书是否有任何问题。 https://www.sslshopper.com/ssl-checker.html –
证书是自签名的。除非将证书作为可信证书手动添加到其Web浏览器,否则用户在访问此网站时会收到警告。您可以通过购买可信SSL证书来解决此错误 –
根据你的意见,你得到一个无效的证书错误,并且你正在使用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];
}
}
我应该在哪里写这段代码? –
在你的NSURLConnectionDelegate中。放一些NSLog调用来查看控件是否到达那里供您参考。 –
没有委托被调用。 –
可以调用使用该设备的浏览器相同的网址是什么? –
它给我一个错误:无法验证服务器身份 –
你打开此网址在网络视图或使用它来使用NSURLSession或NSURLConnection类进行网络API调用?如果可以的话,你也介意分享完整的网址吗?看起来您需要处理身份验证挑战,因为您看到的错误是由于身份不明的证书。在这种情况下,您必须实现在连接期间调用的某些委托方法,并相应地处理它们。 –