How to get the html content from UIWebView?
Now, when UIWebView is deprecated, you'll need to use syntax like this:
webView.evaluateJavaScript("document.documentElement.outerHTML") { (html, error) in
guard let html = html as? String else {
print(error)
return
}
// Here you have HTML of the whole page.
}
After that you can create a function like this that'll easily get HTML from webView:
func getHTML(_ completion: @escaping (String) -> ()) {
webView.evaluateJavaScript("document.documentElement.outerHTML") { (html, error) in
guard let html = html as? String else {
print(error)
return
}
completion(html)
}
}
getHTML { html in
// Here you have HTML string
print(html) // for example...
}
I think you have to evaluate the javascript like this:
let doc = webView.stringByEvaluatingJavaScriptFromString("document.documentElement.outerHTML")
In this case you get the entire HTML.