美文网首页
关于WKWebView加载HTTPS网页不显示的解决方案

关于WKWebView加载HTTPS网页不显示的解决方案

作者: NanNan | 来源:发表于2021-06-08 14:49 被阅读0次

WKNavigationDelegate的代理方法

- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(nonnull NSURLAuthenticationChallenge *)challenge completionHandler:(nonnull void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler {
    //判断服务器采用的验证方法
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
        if([challenge previousFailureCount] == 0){
            //如果没有错误的情况下 创建一个凭证,并使用证书
            NSURLCredential*credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
            completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
        } else {
            //验证失败,取消本次验证
           completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
        }
    } else {
        completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
    }
}

相关文章

网友评论

      本文标题:关于WKWebView加载HTTPS网页不显示的解决方案

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