Events Basics

The game webview passes a set of helpful events to the native wrapper. Here is how they are sent and how to capture them.

iOS

Javascript sends the event from the webview

window.webkit.messageHandlers.onWebViewEvent.postMessage('single_event');

Swift delegate

private lazy var userContentController = webview.configuration.userContentController
private let userScripts = "onWebViewEvent"
// set your delegate in your viewcontroller
webview.navigationDelegate = self
// add
self.userContentController.add(self, name: userScripts)
// remove
self.userContentController.removeAllUserScripts()
// handle
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
    switch message.name {
        case "onWebViewEvent":
        // your data
        let body = message.body    
        default:
             break
    }
}

Android

Javascript sends the event from the webview

window.Android.onWebViewEvent('single_event');

Kotlin

class  WebAppInterface(){
    @JavascriptInterface
    fun onWebViewEvent(eventName:String){
    //handle event
    }
}
webView.addJavascriptInterface(WebAppInterface(),"Android")

Last updated

Was this helpful?