Skip to content Skip to sidebar Skip to footer

Getting The Html Source Code Of A Loaded Uiwebview

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

Solution 1:

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).

Solution 2:

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!

Post a Comment for "Getting The Html Source Code Of A Loaded Uiwebview"