开发者

How can I call objective-c code from within an html file in a UIWebView?

开发者 https://www.devze.com 2023-03-05 03:42 出处:网络
So I know this type of thing is easily achieved in Java using JSP files, but I am wondering how I can mix code and html inside an iOS view. The reason I need to do that is that I have (local) html con

So I know this type of thing is easily achieved in Java using JSP files, but I am wondering how I can mix code and html inside an iOS view. The reason I need to do that is that I have (local) html content being loaded into a UIWebView, and if they click on an html button, I need it to fire like an IBAction for example, and do some stuf开发者_运维知识库f locally.

Is this possible in XCode? What am I missing?


You can not have Objective-C code mixed with an HTML file that runs locally on a UIWebView. That said, there are some workarounds that may fit your needs.

One that I've used, is to have the HTML link to a custom URL, and detect that on the appropriate method in the UIWebView delegate class, as follows:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

In that method, you would check for the URL schema you created by means of checking the request parameter, and then decide what selector to code on your Objective-C code.


The following blog has shown a way to do this

http://davinccoder.wordpress.com/2011/06/28/call-ios-method-for-html-button-click/


This is possible by using JavaScript event handlers that call your objective-c methods via the javascript bridge.

0

精彩评论

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