0
I followed the code snippet by @iUrii from this post: https://stackoverflow.com/a/63645453/981806. However, my app crashes and gets this error on some sites:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Completion handler passed to -[WebSource.MainViewController webView:didReceiveAuthenticationChallenge:completionHandler:] was not called' terminating with uncaught exception of type NSException (lldb)
Code snippet below:
func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
var error: CFError?
if #available(iOS 12.0, *) {
DebugLog(tag: TAG, info: "WebView: didReceive challenge - ios12 or higher")
if let trust = challenge.protectionSpace.serverTrust, !SecTrustEvaluateWithError(trust, &error) {
// OK
let ok = UIAlertAction(title: "OK", style: .default) { _ in
let exceptions = SecTrustCopyExceptions(trust)
SecTrustSetExceptions(trust, exceptions)
completionHandler(.useCredential, URLCredential(trust: trust))
}
// Cancel
let cancel = UIAlertAction(title: "Cancel", style: .cancel) { _ in
completionHandler(.cancelAuthenticationChallenge, nil)
}
// Show prompt
let message = error!.localizedDescription + "\nDo you want to continue?"
let alert = UIAlertController(title: "SSL Error", message: message, preferredStyle: .alert)
alert.addAction(ok)
alert.addAction(cancel)
DispatchQueue.main.async {
self.present(alert, animated: true, completion: nil)
}
}
else {
let cred = URLCredential(trust: challenge.protectionSpace.serverTrust!)
completionHandler(.useCredential, cred)
}
} else {
DebugLog(tag: TAG, info: "WebView: didReceive challenge - fallback older versions")
let cred = URLCredential(trust: challenge.protectionSpace.serverTrust!)
completionHandler(.useCredential, cred)
}
}
On some sites like badssl.com, the alert dialog is displaying fine and it was able to wait for user action from the dialog. Please help on what am I missing here. Thanks!