美文网首页
2021WKWebView绕过https验证最新方式

2021WKWebView绕过https验证最新方式

作者: OneKeyV | 来源:发表于2021-08-13 15:24 被阅读0次

    试过几年前的若干种方式全部无用,Info.plist添加白名单、添加NSURLRequest分类都会报错:

    "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “xxxx” which could put your confidential information at risk.
    

    正确方式:添加WKNavigationDelegate方法

    - (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler {
        if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
            NSURLCredential *credential = [[NSURLCredential alloc]initWithTrust:challenge.protectionSpace.serverTrust];
            completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
        }
    }
    
    - (nullable WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures {
        if (!navigationAction.targetFrame.isMainFrame) {
            [self.webView loadRequest:navigationAction.request];
        }
        return nil;
    }
    

    相关文章

      网友评论

          本文标题:2021WKWebView绕过https验证最新方式

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