Getting HTML elements value from UIWebView

Recently, we came across a problem where we had to fetch some html values from UIWebView. The blog lists source code for extracting a few common elements from UIWebView. The instance method stringByEvaluatingJavaScriptFromString of UIWebView comes in hefty to find such a value.This function will grab the current HTML contents of the view using the Document Object Model, parse the Javascript, then give it to you as an NSString of HTML. Examples To get string from the title of the HTML page:
NSString *currentURL = [theWebView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('title')[0].innerHTML;"];
To get string from class name:
NSString *someHTML = [theWebView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('class name')[0].innerHTML;"];
To get string from element id:
NSString *html = [theWebView stringByEvaluatingJavaScriptFromString:@"document.getElementById('div id').textContent=''"];
theWebView – UIWebView object on which html page is loaded. This approach comes in pretty handy when you want minimal user interaction in some browser savvy process(such as openauth authentication) and hence resulting in a better User Experience. Enjoy the UIWebView awesomeness !!