How to get html content from UIWebView?

I am loading a webpage 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 the document just returns an empty string ( Optional("") ). The window.location.href part is working fine. What am I doing wrong?

+6
source share
3 answers

I think you should evaluate javascript as follows:

 let doc = webView.stringByEvaluatingJavaScriptFromString("document.documentElement.outerHTML") 

In this case, you get all the HTML.

+11
source

I use jqxWidget to display analytics (diagrams) in UIWebView, and my requirement is to display the same diagram in full screen mode when the user clicks "Preview" on another FullScreenViewController on the iPad. When I use the above solution, it displays a chart with no color (black).

I do not want to reload dynamic content that has already been loaded into the previous UIWebView object.

0
source

I found a solution to the problem regarding jqxWidget. This UIWebView content referenced the .js and .css files that are already included in the application. I just added the base url of the MainBundle application.

 NSString *html = [self.webview1 stringByEvaluatingJavaScriptFromString: @"document.documentElement.outerHTML"]; //document.body.innerHTML NSString *path = [[NSBundle mainBundle] bundlePath]; NSURL *baseURL = [NSURL fileURLWithPath:path]; [objFullScreenViewCntrl.webview2 loadHTMLString:html baseURL:baseURL]; 
0
source

Source: https://habr.com/ru/post/984197/


All Articles