views:

79

answers:

1

I am building an iPad app that loads local html files in a UIWebView. I have an int stored in a view controller that I want to use to determine what content to load into the html document.

Additionally, I want to have clickable links in the html that create generate new, smaller UIWebViews on the highest layer, like a "pop-up".

How does one send data back and forth from Obj-C to Javascript?

+2  A: 

You can send data from the Cocoa layer to the JavaScript layer by using the stringByEvaluatingJavaScriptFromString: method in UIWebView.

The Cocoa layer can also "intercept" link clicks by implementing the UIWebViewDelegate protocol in your view controller; when a link is clicked, the delegate method webView:shouldStartLoadWithRequest:navigationType: will be called, at which point the Cocoa layer can do the kind of "pop-up" action you're looking for.

(Although I would ask you why you want to generate pop-ups like this. My gut feeling tells me that this will look and feel quite annoying from the user's point of view.)

Shaggy Frog
Perhaps pop-up was a poor word choice. I want users to be able to click on certain words, opening up a small view on top which will give more information about the selected word. (Image, definition, etc.)
Chris
@Chris the answer I gave you will apply to that scenario as well. The point is knowing when a user has tapped on a link, and then acting.
Shaggy Frog
Yes it will. Thank you very much for the answer. My one concern is that the delegate method is called whenever a user clicks any link, but doesn't retrieve any information about what link is clicked, (address, text, etc.) so it won't help if I have multiple links in the same document, which should each present different content.
Chris
@Chris you're wrong there. If you look at the method closely, you'll see that one of the parameters is a `NSURLRequest` object, which will contain information about the link that was clicked.
Shaggy Frog
Sir you are my hero, and I feel dumb. Thanks a bunch.
Chris
@Chris no worries. If you found this answer helpful, feel free to mark it as answered and give it an "upvote".
Shaggy Frog
Marked as answered, I have no reputation so can't upvote.
Chris