代码之家  ›  专栏  ›  技术社区  ›  Lorenzo B

wkwebview无法加载带有错误证书公用名的网页无效

  •  0
  • Lorenzo B  · 技术社区  · 7 年前

    我用的是 WKWebView 在存在自签名证书的测试环境中加载网页。

    wkwebview网站 似乎无法加载该网页,因为证书存在以下问题:

    错误证书公用名无效

    使用Safari,无论是iOS还是MacOS,如果我打开页面,就会得到以下消息:

    enter image description here

    如果我决定继续,我可以正确加载页面。相反,与 wkwebview网站 ,没有允许绕过此类检查的api。

    我试图通过使用以下代码片段来信任所有证书,但什么都没有发生:

    func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
    
        if let serverTrust = challenge.protectionSpace.serverTrust {
            completionHandler(.useCredential, URLCredential(trust: serverTrust))
        } else {
            completionHandler(.cancelAuthenticationChallenge, nil)
        }
    }
    

    在我的 Info.plist 我设置的文件 NSAllowsArbitraryLoadsInWebContent YES ,以及 NSAllowsArbitraryLoads 向后兼容。

    知道吗?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Lorenzo B    7 年前

    我发现了问题。

    以前,我使用这种技术注入第三部分cookie以加载网页。

    How to set WKWebView cookie accept policy?

    在ios 12中,由于 https://bugs.webkit.org/show_bug.cgi?id=188691 .

    我绞尽脑汁想办法…