首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在kCFStreamErrorDomainSSL 7上绕过自签名证书的iOS错误

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

Stack Overflow用户
提问于 2014-02-12 05:14:05
回答 2查看 21.5K关注 0票数 11

我正在尝试将具有自签名证书的HTTPS网页加载到UIWebView中。使用像this onethis one这样的提示,它可以在iOS 6下工作,而在iOS 7中则不起作用。

根据链接到堆栈溢出的问题,我还使用NSURLConnection首先尝试并通过自签名证书--所有这些都是在试图在UIWebView中加载URL之前。

在iOS 7中尝试相同的操作时,我会得到以下错误:

2014-02-12 16:00:08.367 WebView24176:5307 NSURLConnection/CFURLConnection负载失败(kCFStreamErrorDomainSSL,-9802) 2014-02-12 16:00:08.370 WebView24176:70b出现SSL错误,无法与服务器建立安全连接。

在iOS 7中,有什么办法可以让它工作吗?目前我正在使用first example

EN

回答 2

Stack Overflow用户

发布于 2014-06-19 14:03:11

请按以下连结:

in UiWebView - NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -108)

代码语言:javascript
复制
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];
}
票数 17
EN

Stack Overflow用户

发布于 2015-10-03 10:27:59

在类中添加此方法:

代码语言:javascript
复制
-(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];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21719240

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档