I’ve mentioned in the past instances when you may want to use a UIWebView for tasks such as rendering .SVG files, but there could likely be instances when you want to go the other way. I see questions on how to do this popping up time and time again.
Communicating with the webview itself is easy as you can execute Javascript code using the UIWebView stringByEvaluatingJavaScriptFromString but going the other way things can get a bit hacky.
I’ve come across a very easy to use Javascript and Objective-C library that allows for two way communication with a UIWebView with just a few lines of code.
The library is from Marcus Westin and can be found with full documentation, and an easy to understand example here:
https://github.com/marcuswestin/WebViewJavascriptBridge
If you ever find yourself struggling to get some Javascript running in a UIWebView to communicate with your Objective-C code you can use this library which makes things a snap, and of course since it’s open source you can take a look at how things are done.
©2011 iPhone, iOS 4, iPad SDK Development Tutorial and Programming Tips. All Rights Reserved.
.