开发者

Getting Objective-C to talk to JavaScript with UIWebView

开发者 https://www.devze.com 2023-01-16 09:53 出处:网络
I am building an iPad app that loads local html files开发者_JAVA百科 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 docume

I am building an iPad app that loads local html files开发者_JAVA百科 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?


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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号