Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the html content from UIWebView?

I'm loading a web page using UIWebView

let urlStr = "http://duckduckgo.com"
let urlReq = NSMutableURLRequest(URL: NSURL(string: urlStr)!)
webView.loadRequest(urlReq)

Then, when the page has finished loading, I want to access the html content

func webViewDidFinishLoad(webView: UIWebView) {
    let href = webView.stringByEvaluatingJavaScriptFromString("window.location.href")
    println("window.location.href  = \(href)")

    let doc = webView.stringByEvaluatingJavaScriptFromString("document")
    println("document = \(doc)")
}

But document just return an empty string (Optional("")). The window.location.href part is working fine. What I'm I doing wrong?

like image 409
ihatetoregister Avatar asked Mar 24 '15 14:03

ihatetoregister


People also ask

How do I display HTML data in swift WebView?

How to load a HTML string into a WKWebView or UIWebView: loadHTMLString() If you want to load resources from a particular place, such as JavaScript and CSS files, you can set the baseURL parameter to any URL .

How do I import HTML into WKWebView?

Load Local HTML File to a WKWebViewlet myUrl = myProjectBundle. url(forResource: "my-html-file", withExtension: "html")! where: my-html-file – is the name of HTML file you want to load into WKWebView from your App Bundle.

What is the difference between UIWebView and WKWebView?

Unlike UIWebView, which does not support server authentication challenges, WKWebView does. In practical terms, this means that when using WKWebView, you can enter site credentials for password-protected websites.

How do I use WKWebView?

Here's how: Open the XIB or Storyboard you want to add the web view to in Interface Builder. Find the web view or WKWebView in the Object Library at the bottom-left of Interface Builder. Drag-and-drop a WKWebView object from the Object Library to your view controller's canvas, and adjust its size and position.


2 Answers

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.

like image 99
Niccolò Passolunghi Avatar answered Oct 19 '22 19:10

Niccolò Passolunghi


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...
}
like image 6
Roman Esin Avatar answered Oct 19 '22 19:10

Roman Esin