« Check our QuickLook P… | Home | Trigger FileMaker Scr… »

Custom JavaScript messages to WKWebViewControlMBS

When using WKWebViewControlMBS control in your Xojo projects, you can use WebKit 1.x (32-bit) and WebKit 2.x (64-bit) transparently. Depending on the bit setting of your Mac application, the plugin uses either the newer or older classes from WebKit.

Now with macOS 10.10 (and iOS 8.0) Apple introduced the ability to install custom JavaScript message handlers for WKWebView. We just added them for our plugins to call back from JavaScript to Xojo code. First you call in your 64-bit Mac app the addScriptMessageHandler method on the control to register a name:

browser.addScriptMessageHandler "test"

Next you can use it in Javascript, e.g. via evaluate:


As you see, we get for our message handler a new entry in the namespace called window.webkit.messageHandlers.test. This allows us to call postMessage method there and this triggers the didReceiveScriptMessage event in our control. So we implement an event to show via msgbox what we got:

Sub didReceiveScriptMessage(Body as Variant, name as String)
  MsgBox name+EndOfLine+body.StringValue
End Sub

This works nice in first tests and we'd love to see what you can do with this technique. Data from JavaScript is converted to Objective-C types and than converted to Xojo data types. So arrays, numbers, dictionaries and strings should work well. Please try with next prerelease or email us for a copy today.
15 12 17 - 12:17
No comments

Remember personal info?

Emoticons / Textile

Hide email:

Small print: All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.