How to retrieve the HTML contained inside a UIWebView in iOS [#iosdev]

Recently on a project I needed to see what was going on inside the UIWebView that I was working with. Turns out it is very simple to retrieve and then log the HTML contained inside the UIWebView, using the stringByEvaluatingJavaScriptFromString method of the UIWebView, coupled with a very simple JavaScript call…

- (void)logDocumentHtml {
    NSString *html = [self.chatWebView stringByEvaluatingJavaScriptFromString: @"document.documentElement.innerHTML"];
    NSLog(@"document HTML:\n%@\n", html);
}

Hope this helps!