objective_man objective_man - 2 years ago 186
iOS Question

UIWebView Source code

I have an IBAction on a button with the following code that I am trying to use to retrieve the source code of a UIWebView:

- (IBAction)loadInAWebView:(id)sender {
[self.webView loadHTMLString:[NSString stringWithFormat:@"Hello: %@", _name.text] baseURL:nil];

NSString *yourHTMLSourceCodeString = [_webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.innerHTML"];

NSLog(@"%@", yourHTMLSourceCodeString);

The use of the code above is from the reference I saw at this link: Getting the HTML source code of a loaded UIWebView.

However, the NSLog function always returns the following:


This obviously does not reflect the actual source code of the UIWebView that in my mind should be:

<head></head><body>Hello, name</body>

Indeed, the UIWebView returns the "Hello, name" content but the related source code does not.

How can I edit the code above to retrieve the real UIWebView source code?

Answer Source

You need implement UIWebViewDelegate methods
- (void)webViewDidFinishLoad:(UIWebView *)webView and set self.webView.delegate = self;
Put your code NSString *yourHTMLSourceCodeString = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.innerHTML"]; to this method

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download