【问题标题】:Bypass kCFStreamErrorDomainSSL error for self-signed certificates on iOS 7绕过 iOS 7 上自签名证书的 kCFStreamErrorDomainSSL 错误
【发布时间】:2014-03-10 06:22:12
【问题描述】:
我正在尝试将具有自签名证书的 HTTPS 网页加载到 UIWebView。使用 this one 或 this one 之类的提示,它可以在 iOS 6 下运行。在 iOS 7 中同样不可用。
根据链接到 Stack Overflow 的问题,我还使用 NSURLConnection 首先尝试通过自签名证书——这一切都在尝试在 UIWebView 中加载 URL 之前完成。
在 iOS 7 中尝试相同操作时,我收到以下错误:
2014-02-12 16:00:08.367 WebView[24176:5307] NSURLConnection/CFURLConnection HTTP 加载失败(kCFStreamErrorDomainSSL,-9802)
2014-02-12 16:00:08.370 WebView[24176:70b] 发生 SSL 错误,无法与服务器建立安全连接。
是否有解决方法可以让它在 iOS 7 中运行?目前我正在使用first example。
【问题讨论】:
标签:
ios7
https
nsurlconnection
ssl-certificate
【解决方案1】:
请点击链接:
in UiWebView - NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -108)
BOOL _Authenticated;
NSURLRequest *_FailedRequest;
#pragma UIWebViewDelegate
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
BOOL result = _Authenticated;
if (!_Authenticated) {
_FailedRequest = request;
NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[urlConnection start];
}
return result;
}
#pragma NSURLConnectionDelegate
-(void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
NSURL* baseURL = [NSURL URLWithString:@"your url"];
if ([challenge.protectionSpace.host isEqualToString:baseURL.host]) {
NSLog(@"trusting connection to host %@", challenge.protectionSpace.host);
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
} else
NSLog(@"Not trusting connection to host %@", challenge.protectionSpace.host);
}
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)pResponse {
_Authenticated = YES;
[connection cancel];
[webvw loadRequest:_FailedRequest];
}
【解决方案2】:
在你的类中添加这个方法:
-(void) connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
{
NSURL* baseURL = [NSURL URLWithString:@"yourURL"];
if ([challenge.protectionSpace.host isEqualToString:baseURL.host])
{
SecTrustRef trust = challenge.protectionSpace.serverTrust;
NSURLCredential *cred = [NSURLCredential credentialForTrust:trust];
[challenge.sender useCredential:cred forAuthenticationChallenge:challenge];
}
else
NSLog(@"Not trusting connection to host %@", challenge.protectionSpace.host);
}
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}