Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the HTML source code of a loaded UIWebView [duplicate]

How can I get the HTML source code of a UIWebView and store it in NSString?

like image 800
Wasim A. Avatar asked Sep 10 '25 02:09

Wasim A.


2 Answers

If you want the source code of an already loaded webview, you can get it like this:

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

Update: method above works most of the time. But I recently was in a situation where I needed all the code source, not just the body.

Here is a better way:

    NSString *yourHTMLSourceCodeString = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.outerHTML"];       

(It won't reload the webview).

like image 61
Guillaume Avatar answered Sep 12 '25 18:09

Guillaume


I am not sure that what exactly you need..........but as per your last comment you will get that html data from google like this...

     NSData *companyData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"]];
     NSString *responseString = [[NSString alloc] initWithData:companyData encoding:NSUTF8StringEncoding];

    self.yourTextView.text = responseString; // response string contains that data
    [companyData release];

Good Luck!

like image 39
Sudhanshu Avatar answered Sep 12 '25 17:09

Sudhanshu