title: Swift WKWebView goBack and goForward No Longer Work
body: <p>I've never seen an error like this before. The below code worked perfectly previously. I don't see any change that I would have made that would change this. Now, goBack and goForward do not work as intended. What happened? This is extremely bizarre.</p>
  
  <p>I have tried tracing this through, all sorts of stuff, and cannot understand why it does not go back when clicked. I even tried adding the webView.reload() after back is clicked, but that does not work either.</p>
  
  <p>I get this error: <code>Error Domain=NSURLErrorDomain Code=-999 "(null)"</code> followed by the page I was trying to visit. Here is my class:</p>
  
  <pre><code>class WebPageModalViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
  
      @IBOutlet weak var mainView: UIView!
      @IBOutlet weak var primaryLabel: UILabel!
      @IBOutlet weak var secondaryLabel: UILabel!
      @IBOutlet weak var backButtonImageView: UIImageView!
      @IBOutlet weak var forwardButtonImageView: UIImageView!
  
      @IBAction func didClickClose(_ sender: Any) {
          IHProgressHUD.dismiss()
          dismiss(animated: true, completion: nil)
      }
      @IBAction func didClickOpenInBrowser(_ sender: Any) {
          URL.openURL(self.url)
      }
  
      var url: String
      var webView: WKWebView!
  
      init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?, url: String) {
          self.url = url
          super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
      }
  
      required init?(coder aDecoder: NSCoder) {
          fatalError("init(coder:) has not been implemented")
      }
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          let backGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
          backButtonImageView.isUserInteractionEnabled = true
          backButtonImageView.addGestureRecognizer(backGestureRecognizer)
          let forwardGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
          forwardButtonImageView.isUserInteractionEnabled = true
          forwardButtonImageView.addGestureRecognizer(forwardGestureRecognizer)
  
          self.primaryLabel.text = ""
          self.secondaryLabel.text = ""
  
          webView.scrollView.delegate = self
  
          let myURL = URL(string:url)
          let myRequest = URLRequest(url: myURL!)
  
          webView.load(myRequest)
  
          setTintColor(backButtonImageView, enabled: false)
          setTintColor(forwardButtonImageView, enabled: false)
  
      }
  
      override func viewDidAppear(_ animated: Bool) {
          super.viewDidAppear(animated)
      }
  
      override var preferredStatusBarStyle : UIStatusBarStyle {
          return .lightContent
      }
  
      override func viewWillDisappear(_ animated: Bool) {
          super.viewWillDisappear(animated)
          IHProgressHUD.dismiss()
      }
  
      override func loadView() {
          super.loadView()
  
          let webConfiguration = WKWebViewConfiguration()
          webView = WKWebView(frame: .zero, configuration: webConfiguration)
          webView.uiDelegate = self
          webView.navigationDelegate = self
  
          webView.addObserver(self, forKeyPath: #keyPath(WKWebView.title), options: .new, context: nil)
          webView.addObserver(self, forKeyPath: #keyPath(WKWebView.canGoForward), options: .new, context: nil)
          webView.addObserver(self, forKeyPath: #keyPath(WKWebView.canGoBack), options: .new, context: nil)
  
          if (webView != nil) {
              mainView.addConstrained(subview: webView!)
          }
      }
  
      // MARK: WkWebview
      func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
          IHProgressHUD.show()
      }
  
      func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
          IHProgressHUD.dismiss()
      }
  
      func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
          IHProgressHUD.dismiss()
          BannerManager.errorLoadingWebpage.show()
          dismiss(animated: true, completion: nil)
      }
  
      func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
          guard let failingUrlStr = (error as NSError).userInfo["NSErrorFailingURLStringKey"] as? String  else { return }
          let failingUrl = URL(string: failingUrlStr)!
  
          switch failingUrl {
              // Needed to open Appstore-App
              case _ where failingUrlStr.hasPrefix("itms-appss://"):
                  if UIApplication.shared.canOpenURL(failingUrl) {
                      UIApplication.shared.open(failingUrl, options: [:], completionHandler: nil)
                      IHProgressHUD.dismiss()
                      dismiss(animated: true, completion: nil)
                      return
                  } else {
                      self.webView(webView, didFail: navigation, withError: error)
                  }
              default: self.webView(webView, didFail: navigation, withError: error)
          }
      }
  
  //    func webView(_ webView: WKWebView,
  //                 decidePolicyFor navigationAction: WKNavigationAction,
  //                 decisionHandler: @escaping (WKNavigationActionPolicy) -&gt; Void) {
  //        
  //        // if the url is not http(s) schema, then the UIApplication open the url
  //        if let url = navigationAction.request.url,
  //            !url.absoluteString.hasPrefix("http://"),
  //            !url.absoluteString.hasPrefix("https://"),
  //            UIApplication.shared.canOpenURL(url) {
  //            
  //            UIApplication.shared.open(url, options: [:], completionHandler: nil)
  //            // cancel the request
  //            decisionHandler(.cancel)
  //        } else {
  //            // allow the request
  //            decisionHandler(.allow)
  //        }
  //    }
  
      override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
          if keyPath == "title" {
              self.primaryLabel.text = webView.title
              self.secondaryLabel.text = webView.url?.absoluteString
          } else if keyPath == "canGoBack" {
              setTintColor(backButtonImageView, enabled: webView.canGoBack)
          } else if keyPath == "canGoForward" {
              setTintColor(forwardButtonImageView, enabled: webView.canGoForward)
          }
      }
  
      // MARK: Private Funcs
      private func setTintColor(_ image:UIImageView, enabled:Bool) {
          image.tintColor = enabled ? UIColor.textSecondary : UIColor.ECHLightGrey
      }
  
      @objc func imageTapped(tapGestureRecognizer: UITapGestureRecognizer) {
          let tappedImage = tapGestureRecognizer.view as! UIImageView
          if tappedImage == backButtonImageView {
              webView.goBack()
          } else if tappedImage == forwardButtonImageView {
              webView.goForward()
          }
      }
  }
  
  extension WebPageModalViewController : UIScrollViewDelegate {
  
      func scrollViewDidScroll(_ scrollView: UIScrollView) {
          scrollView.contentOffset.x = 0
      }
  }
  </code></pre>
  
  <p>Any thoughts would be great!</p>