Custom JavaScript messages to WKWebViewControlMBS
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:
browser.EvaluateJavaScript("window.webkit.messageHandlers.test.postMessage('Hello');")
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.