gpt4 book ai didi

ios - 在 WKWebView 中允许未经验证的 ssl 证书

转载 作者:IT王子 更新时间:2023-10-29 07:55:27 49 4
gpt4 key购买 nike

我试图在 iOS 8 的 WKWebView 中加载带有自签名证书的 HTTPS url,但它一直失败。与 UIWebView 一起使用的解决方法(使用来自 NSUrlRequest 的 setAllowsAnyHTTPSCertificate)似乎不起作用。有谁知道任何解决方法?

我不需要对 AppStore 有效的解决方案,因为我只需要在开发阶段访问自签名证书站点,而不是在生产阶段,但这确实是开发和测试服务器实例的问题。

提前谢谢你。

最佳答案

这在 iOS 9 中已修复! WKWebView 最终在 WKNavigationDelegate 上调用了 webView(_:didReceiveAuthenticationChallenge:completionHandler:)。不幸的是,如果您在 iOS 8 设备上运行 Xcode 7 中构建的代码,这将不起作用(至少在我的初始测试中不会)。

在我下面的例子中,我实际上并没有对证书做任何事情,只是让它通过而不做任何进一步的验证(对于生产代码来说显然是一个糟糕的计划)。参见 Apple's docs ( list 3),了解他们希望您在此处执行的操作的更多详细信息。

swift :

func webView(webView: WKWebView, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge,
completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void) {
let cred = NSURLCredential.init(forTrust: challenge.protectionSpace.serverTrust!)
completionHandler(.UseCredential, cred)
}

swift 3:

let cred = URLCredential(trust: challenge.protectionSpace.serverTrust!)
completionHandler(.useCredential, cred)

swift 4:

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

objective-C

NSURLCredential * credential = [[NSURLCredential alloc] initWithTrust:[challenge protectionSpace].serverTrust];
completionHandler(NSURLSessionAuthChallengeUseCredential, credential);

关于ios - 在 WKWebView 中允许未经验证的 ssl 证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27100540/

49 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com