美文网首页
强制打开HTTPS证书无效的网址

强制打开HTTPS证书无效的网址

作者: 黑暗森林的歌者 | 来源:发表于2018-08-22 12:03 被阅读127次

强制打开HTTPS证书无效的网址

有时候一些特定的网页是使用HTTPS的,但是有可能HTTPS证书过期失效,这时候如果直接使用web打开,就会报错。

Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid.

可以在web的代理方法中执行下面的方法,强制信任无效的HTTPS证书

代码示例是WKWebView

- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler{
    
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
        
        NSURLCredential *card = [[NSURLCredential alloc]initWithTrust:challenge.protectionSpace.serverTrust];
        
        completionHandler(NSURLSessionAuthChallengeUseCredential,card);
        
    }
}

相关文章

网友评论

      本文标题:强制打开HTTPS证书无效的网址

      本文链接:https://www.haomeiwen.com/subject/nsweiftx.html