在iOS 7上绕过自签名证书的kCFStreamErrorDomainSSL错误

时间:2014-02-12 05:14:05

标签: ios7 https nsurlconnection ssl-certificate

我正在尝试将具有自签名证书的HTTPS网页加载到UIWebView中。使用this onethis 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

2 个答案:

答案 0 :(得分:17)

请点击链接:

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];
}

答案 1 :(得分:0)

在您的课程中添加此方法:

-(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];
}