Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I expose an Objective-C function to JavaScript using WebKit on Mac OS X?

I understand to do this on the iPhone you need to trap link requests (as per my other iPhone question UIWebView Expose JavaScript) and you can easily do the reverse and access JavaScript from Obj-C code.

However, I would like to have JavaScript be able to call some Objective-C functions that would somehow be registered with WebKit. I assume that you can do this better than trapping links like on the iPhone as on Mac OS X we have access to the full WebKit.

I wish to basically do the reverse of Using JavaScript from Objective-C

Update: For example is it possible to expose the objective-c method in JavaScript like self.external.objcmethod();

like image 845
Luke Avatar asked May 10 '26 14:05

Luke


1 Answers

Have a look at my answer to this question, which is very similar. I provide code that shows you exactly how to do what you want.

Essentially, you need to implement the WebScripting protocol as per the documentation.

You don't need to define your own URL scheme handler, that is only necessary for iPhone applications. On the Mac the WebView object is many orders of magnitude more powerful than UIWebView on the iPhone and allows full bridging from JavaScript to Objective-C and vice versa.

like image 112
Rob Keniger Avatar answered May 13 '26 04:05

Rob Keniger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!