iosMain.com.multiplatform.webview.jsbridge.WKJsMessageHandler.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of compose-webview-multiplatform Show documentation
Show all versions of compose-webview-multiplatform Show documentation
WebView for JetBrains Compose Multiplatform
package com.multiplatform.webview.jsbridge
import com.multiplatform.webview.util.KLogger
import kotlinx.serialization.json.Json
import platform.WebKit.WKScriptMessage
import platform.WebKit.WKScriptMessageHandlerProtocol
import platform.WebKit.WKUserContentController
import platform.darwin.NSObject
/**
* Created By Kevin Zou On 2023/11/1
*/
/**
* A JS message handler for WKWebView.
*/
class WKJsMessageHandler(private val webViewJsBridge: WebViewJsBridge) :
WKScriptMessageHandlerProtocol,
NSObject() {
override fun userContentController(
userContentController: WKUserContentController,
didReceiveScriptMessage: WKScriptMessage,
) {
val body = didReceiveScriptMessage.body
val method = didReceiveScriptMessage.name
KLogger.info { "didReceiveScriptMessage: $body, $method" }
(body as String).apply {
val message = Json.decodeFromString(body)
KLogger.info {
"WKJsMessageHandler: $message"
}
webViewJsBridge.dispatch(message)
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy