I tried to execute code in android from the web view through events. So I implemented the good way with a JavascriptInterface, but since I have a iOS Application, I wanted to use the same method. So is this possible to use the message handler on Android (just a simple JS event) rather than the Bridging of the interface ? I also tried to the other way : Make a bridge on iOS the same way of Android, but it doesn't work with WKWebview (but it's another problem) Thanks !
It can also communicate back to your app using script messages using the WKScriptMessageHandler protocol. While user scripts may let you inject JavaScript code into your webpage, script messages let you call native code from JavaScript. To do this, there are a few steps on the iOS side:
WebMessageListener APIs provide a simple and secure mechanism to establish communication between web contents and the WebView embedder app. MultiProcessEnabled API to check if WebView is running in multi process mode. androidx.webkit:webkit:1.3.0-alpha03 is released. Version 1.3.0-alpha03 contains these commits.
By including these message handlers into your WKUserContentController object, your web view will define a new function window.webkit.messageHandlers. name .postMessage ( messageBody) that can be called in all frames. Here’s a simple example of adding a message handler called “test” that, when, called in your script tag, will print “Hello, world!”:
Work with modern WebView APIs on Android 5 and above. To add a dependency on Webkit, you must add the Google Maven repository to your project. Read Google's Maven repository for more information. Add the dependencies for the artifacts you need in the build.gradle file for your app or module:
This is not recommended, if you have the option to change your JS-code and add a conditional handler, you should do that instead.
If you have implemented a handler in iOS using userContentController
and want to reuse it in Android, you can inject JS to replace the window.webkit.messageHandlers
property with your android equivalent.
Say this is your swift code:
webView.configuration.userContentController.add(self, name: "iosListener")
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
//handle message
}
And your JS code:
window.webkit.messageHandlers.iosListener.postMessage("some message")
You can reuse it in android/kotlin without changing your JS code like this:
inner class MyJsInterface() {
@JavascriptInterface
fun postMessage(value: String) {
//handle message
}
}
webView.run {
settings.javaScriptEnabled = true
addJavascriptInterface(MyJsInterface(), "AndroidListener")
webViewClient = object : WebViewClient() {
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)
evaluateJavascript("window.webkit = { messageHandlers: { iosListener: window.AndroidListener} }") {}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With