Stephen J.
Stephen J.

Reputation: 3127

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

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 stuff locally.

Is this possible in XCode? What am I missing?

Upvotes: 1

Views: 2179

Answers (3)

raaz
raaz

Reputation: 12500

The following blog has shown a way to do this

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

Upvotes: 4

pgb
pgb

Reputation: 25001

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.

Upvotes: 5

Jakub Hampl
Jakub Hampl

Reputation: 40553

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

Upvotes: 2

Related Questions