【发布时间】:2018-10-22 20:30:22
【问题描述】:
我正在使用WKWebView 在存在自签名证书的测试环境中加载网页。
WKWebView 似乎无法加载该网页,因为证书存在以下问题:
ERR_CERT_COMMON_NAME_INVALID
使用 Safari,iOS 和 macOS,如果我打开页面,我会收到以下消息:
如果我决定继续,我可以正确加载页面。相反,WKWebView 没有允许绕过此类检查的 API。
我尝试使用以下 sn-ps 信任所有证书,但没有任何反应:
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 以具有向后兼容性。
有什么想法吗?
【问题讨论】:
-
您是否尝试在设备上安装本地根证书?您需要通过 Apple Configurator 执行此操作,创建配置文件并将其安装在您的设备上。问题是使用不受信任的根证书签名的证书在任何情况下都不会被信任。
-
@dirkgroten 我要试试。谢谢。你有指南要分享吗?
-
实际上看起来您可以通过电子邮件或 AirDrop 将证书发送给自己然后安装(重要:根证书需要具有 CN - Common Name)。之后,不要忘记明确信任它:参见 here 和 this link for openssl instructions
-
@dirkgroten 谢谢。信任证书的事件不起作用。
-
Info.plist 中的
NSAppTransportSecurity设置怎么样?将NSExceptionAllowsInsecureHTTPLoads设置为YES
标签: ios macos ssl-certificate wkwebview