我使用此以下功能来通过URLSessionDelegate在xcode中执行手动服务器身份验证
func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
#if DEBUG
if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
if challenge.protectionSpace.host == "localhost" {
// At this point you can prevent a domain that is pretending to be a trusted domain by challenging the user to present some credentials or a security mechanism for authentication.
if let serverTrust = challenge.protectionSpace.serverTrust {
let credential = URLCredential(trust: serverTrust)
completionHandler(URLSession.AuthChallengeDisposition.useCredential, credential)
}
}
}
#endif
}
}
但仍然收到此错误=>由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'未将完成处理程序传递给-[health.ViewController webView:didReceiveAuthenticationChallenge:completionHandler:]。如果有人知道如何解决此问题,请告诉我