title: WKWebView Basic auth pop-up appears twice and app crushes
body: <p>I'm trying to add basic auth feature to my iOS app's WKWebView. The basic auth pop-up appears in a right way.<br>
  But after I enter the correct information, the pop-up appears once more, and when I enter information again the app crushes. The console says <code>'NSInternalInconsistencyException', reason: 'Completion handler passed to -[SFSafariViewController.WebViewViewController webView:didReceiveAuthenticationChallenge:completionHandler:] was not called'</code>.<br>
  Behind the second pop-up the website contents already shows up, so I think basic auth pop-up is called in a wrong way.<br>
  Where should I fix?  </p>
  
  <p>Here's my code:  </p>
  
  <pre><code>import UIKit
  import WebKit
  
  class WebViewViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
  
      @IBOutlet weak var webView: WKWebView!
  
      var startUrl = "http://basic-auth-page.com"
      var pageTitle: String?
      var requestUrl: String?
  
      override func loadView() {
          super.loadView()
          webView.uiDelegate = self
          webView.navigationDelegate = self
      }
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          webView.allowsBackForwardNavigationGestures = true
  
          if let url = URL(string: startUrl) {
              let request = URLRequest(url: url)
              webView.load(request)
          }
      }
  
      // open "target_blank" link
      func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -&gt; WKWebView? {
  
          if navigationAction.targetFrame == nil {
              webView.load(navigationAction.request)
          }
          return nil
      }
  
      // for basic auth
      func webView(_ webView: WKWebView, didReceive didReceiveAuthenticationChallenge: URLAuthenticationChallenge, completionHandler: @escaping(URLSession.AuthChallengeDisposition, URLCredential?) -&gt; Void){
          let alertController = UIAlertController(title: "Authentication Required", message: "This is an alert.", preferredStyle: .alert)
          weak var usernameTextField: UITextField!
          alertController.addTextField { textField in
              textField.placeholder = "Username"
              usernameTextField = textField
          }
          weak var passwordTextField: UITextField!
          alertController.addTextField { textField in
              textField.placeholder = "Password"
              textField.isSecureTextEntry = true
              passwordTextField = textField
          }
          alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { action in
              completionHandler(.cancelAuthenticationChallenge, nil)
          }))
          alertController.addAction(UIAlertAction(title: "Log In", style: .default, handler: { action in
              let credential = URLCredential(user: usernameTextField.text!, password: passwordTextField.text!, persistence: URLCredential.Persistence.forSession)
              completionHandler(.useCredential, credential)
          }))
          self.present(alertController, animated: true, completion: nil)
      }
  
      @IBAction func prevBtn(_ sender: UIBarButtonItem) {
          if webView.canGoBack {
              webView.goBack()
          }
      }
  
      @IBAction func nextBtn(_ sender: UIBarButtonItem) {
          if webView.canGoForward {
              webView.goForward()
          }
      }
  
      @IBAction func refreshBtn(_ sender: UIBarButtonItem) {
          webView.reloadFromOrigin()
      }
  
      @IBAction func doneBtn(_ sender: UIBarButtonItem) {
          let initialBoard = storyboard!.instantiateViewController(withIdentifier: "initialBoard")
          self.present(initialBoard, animated: true, completion: nil)
      }
  
      func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
      }
  
  }
  </code></pre>
  
  <p>===edit===<br>
  I found another console warning log when the second pop-up appeared.<br>
  <code>Warning: Attempt to present &lt;UIAlertController: 0x7fa46d88a600&gt;  on &lt;SFSafariViewController.WebViewViewController: 0x7fa46f10b350&gt; which is already presenting &lt;UIAlertController: 0x7fa46e819000&gt;</code> </p>