Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to access the HTML code of a UIWebView, or access a selection, or both?

I'm looking to enable users to import data from websites, without giving away too much of my app, say for example to store StackOverflow questions. (It isn't really StackOverflow questions, but you get the idea, stuff that isn't being sent from a server that is kindly delivering the data in a format intended for my app).

What I'm asking in the most basic sense is can I access the stuff in the current page the user is visiting in a UIWebView (in my app, on a button press), and scan through for keywords and extract text?

Before people start asking, the purpose of this is to get rid of a barrier to switching, by allowing users to get their own content for my app from the internet, since I can't possibly supply enough of it myself.

If this is impossible, could there be a route where my app requests the page from the server directly, not via a UIWebview, (After the user has found the page in a UIWebview) and gets a copy of the source code to deal with?

Thanks for any help!

like image 677
Alex Gosselin Avatar asked Dec 05 '25 20:12

Alex Gosselin


2 Answers

Try this:

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

NSString *selected = [webView stringByEvaluatingJavaScriptFromString:@"document.getSelection()"];

Also, I suggest that you check out this answer, which describes in detail other ways you can retrieve the html of a particular webpage.

like image 170
Jacob Relkin Avatar answered Dec 08 '25 12:12

Jacob Relkin


And here's how to get the current selection:

NSString *selection = [webView stringByEvaluatingJavaScriptFromString:@"document.getSelection()"];
like image 23
Kris Markel Avatar answered Dec 08 '25 14:12

Kris Markel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!