Reading HTML Content from a Uiwebview

Reading HTML content from a UIWebView

The second question is actually easier to answer. Look at the stringWithContentsOfURL:encoding:error: method of NSString - it lets you pass in a URL as an instance of NSURL (which can easily be instantiated from NSString) and returns a string with the complete contents of the page at that URL. For example:

NSString *googleString = @"http://www.google.com";
NSURL *googleURL = [NSURL URLWithString:googleString];
NSError *error;
NSString *googlePage = [NSString stringWithContentsOfURL:googleURL
encoding:NSASCIIStringEncoding
error:&error];

After running this code, googlePage will contain the HTML for www.google.com, and error will contain any errors encountered in the fetch. (You should check the contents of error after the fetch.)

Going the other way (from a UIWebView) is a bit trickier, but is basically the same concept. You'll have to pull the request from the view, then do the fetch as before:

NSURL *requestURL = [[yourWebView request] URL];
NSError *error;
NSString *page = [NSString stringWithContentsOfURL:requestURL
encoding:NSASCIIStringEncoding
error:&error];

EDIT: Both these methods take a performance hit, however, since they do the request twice. You can get around this by grabbing the content from a currently-loaded UIWebView using its stringByEvaluatingJavascriptFromString: method, as such:

NSString *html = [yourWebView stringByEvaluatingJavaScriptFromString: 
@"document.body.innerHTML"];

This will grab the current HTML contents of the view using the Document Object Model, parse the JavaScript, then give it to you as an NSString* of HTML.

Another way is to do your request programmatically first, then load the UIWebView from what you requested. Let's say you take the second example above, where you have NSString *page as the result of a call to stringWithContentsOfURL:encoding:error:. You can then push that string into the web view using loadHTMLString:baseURL:, assuming you also held on to the NSURL you requested:

[yourWebView loadHTMLString:page baseURL:requestURL];

I'm not sure, however, if this will run JavaScript found in the page you load (the method name, loadHTMLString, is somewhat ambiguous, and the docs don't say much about it).

For more info:

  • UIWebView class reference
  • NSString class reference
  • NSURL class reference

How to get the html content from UIWebView?

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.

Get all html data from UIWebView

You can fetch html of a web page by making a get request to the url of the html and you can get html in a NSString object by using following code . You dont even need UIWebview to access html content of a webpage .

NSURLResponse * response = nil;
NSError * error = nil;

NSURLRequest* urlRequest = [NSURLRequest
requestWithURL:[NSURL URLWithString:@"http://www.emdrassociation.org.uk/site.php/profile/emdr/alison_russell.html"]
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:30.0];

NSData* data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
NSString *htmlString = [NSString stringWithUTF8String:[data bytes]];

Get html content from webView in Swift?

You can get the inner text of the div by:

func webViewDidFinishLoad(_ webView: UIWebView) {

guard let text = webView.stringByEvaluatingJavaScript(from: "document.getElementById(\"displayMsg\").innerText") else {
return
}

print(text) // Thanh toán thành công
}

Reading HTML content from UIWebView after editing it

Try using stringByEvaluatingJavaScriptFromString. If you are using jquery, you should be able to use an expression that returns the value of your textfield, i.e.:

NSString* text = [webView stringByEvaluatingJavaScriptFromString:@"$('#myTextFieldId').val();"];

Get HTML from WKWebview in Swift

If you wait until the page has loaded you can use:

webView.evaluateJavaScript("document.documentElement.outerHTML.toString()", 
completionHandler: { (html: Any?, error: Error?) in
print(html)
})

You could also inject some javascript that returns you back the HTML.

let script = WKUserScript(source: javascriptString, injectionTime: injectionTime, forMainFrameOnly: true)
userContentController.addUserScript(script)
self.webView.configuration.userContentController.addScriptMessageHandler(self, name: "didGetHTML")



func userContentController(userContentController: WKUserContentController,
didReceiveScriptMessage message: WKScriptMessage) {

if message.name == "didGetHTML" {
if let html = message.body as? String {
print(html)
}
}
}

The javascript you could inject looks something like:

webkit.messageHandlers.didGetHTML.postMessage(document.documentElement.outerHTML.toString());


Related Topics



Leave a reply



Submit