How can I get the HTML
source code of a UIWebView
and store it in NSString
?
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).
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With