坚持 成长 每日一篇
对于NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9814)网络加载错误!的其中一个原因就是请求需要ssl认证,就是要安装ssl证书,如果你的应用不需要ssl认证加密可以通过添加下面的代码屏蔽掉ssl认证。
Code
只要在每一个Request的NSURLConnectionDelegate的一个回调方法添加如下代码。建议写一个NSURLConnection的子类封装一下
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
{
SecTrustRef trust = challenge.protectionSpace.serverTrust;
NSURLCredential *cred;
cred = [NSURLCredential credentialForTrust:trust];
[challenge.sender useCredential:cred forAuthenticationChallenge:challenge];
return;
}
}