Im using this following function to perform manual server authentication in xcode using URLSessionDelegate
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
}
}
but still im getting this error => Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Completion handler passed to -[health.ViewController webView:didReceiveAuthenticationChallenge:completionHandler:] was not called. if anybody know how to solve this problem please tell me
You’re only calling the completion block if debugging is enabled. You must call the completion handler block every time your delegate method gets called, and for every protection space—typically by requesting default handling. If you fail to call it, the session will wait forever for you to call that block.
Alternatively, you can wrap the entire method with the #ifdef conditional so that it never gets called.