title: Get HTML by evaluating JavaScript in a WKWebView
body: <p>I'm working with a <code>WKWebView</code> in my iOS app and after loading a webpage and I want to get the HTML of gmail.com. I have this expression in a function that I call in ViewDidAppear:</p>
  
  <pre><code>func getHTML() {
      webView.evaluateJavaScript("document.documentElement.outerHTML.toString()", completionHandler: { (html: Any?, error: Error?) in
             print(html)
      })
  }
  </code></pre>
  
  <p>However, when I run this function, I get the following output in the console:</p>
  
  <pre><code>Optional(&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;/body&gt;&lt;/html&gt;)
  </code></pre>
  
  <p>What am I doing wrong? Gmail isn't the only webpage that gives me this result – every page I tried gives me the same output.</p>
  
  <p>Here is all of my code:</p>
  
  <pre><code>// MARK: Outlets
  @IBOutlet weak var webView: WKWebView!
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
      let url = URL(string: "https://www.gmail.com")!
      let request = URLRequest(url: url)
  
      webView.navigationDelegate = self
      self.webView.load(request)
  }
  
  override func viewDidAppear(_ animated: Bool) {
      super.viewDidAppear(animated)
      getHTML() // the function posted above
  }
  </code></pre>