webView:didReceiveAuthenticationChallenge:completionHandler NSException

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!

func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
    var error: CFError?
    if #available(iOS 12.0, *) {
    dispatch_semaphore_t sema = dispatch_semaphore_create(0); // Multiple requests may come to load into web view
    self.pinningCompletion = completionHandler; // strong property
      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))
          dispatch_semaphore_signal(sema);// on completion of alert action


        }

        // Cancel
        let cancel = UIAlertAction(title: "Cancel", style: .cancel) { _ in
          completionHandler(.cancelAuthenticationChallenge, nil)
          dispatch_semaphore_signal(sema);// on completion of alert action


        }

        // 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)
        }
        if (![NSThread isMainThread]) {
            dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
        } else {
            while (dispatch_semaphore_wait(sema, DISPATCH_TIME_NOW)) {
                [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0]];
            }
        }
      }
      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)
    }
  }
webView:didReceiveAuthenticationChallenge:completionHandler NSException
 
 
Q