hotwired / turbo-android

Android framework for making Turbo native apps
MIT License
408 stars 51 forks source link

addJavascriptInterface doesn't work #253

Closed GlebMikhailov closed 1 year ago

GlebMikhailov commented 1 year ago

So, I have this code in MainSessionNavHostFragment :

override fun onSessionCreated() { super.onSessionCreated() session.webView.settings.userAgentString = customUserAgent(session.webView) session.webView.initDayNightTheme() Timber.v("customUserAgent ${customUserAgent(session.webView)}") if (BuildConfig.DEBUG) { session.setDebugLoggingEnabled(true) WebView.setWebContentsDebuggingEnabled(true) } val actionInvoker = WebViewJavascriptInterface(session.webView.context) session.webView.addJavascriptInterface(actionInvoker, "window.appInterface") session.webView.addJavascriptInterface(actionInvoker, "appInterface") }

and this is my WebViewJavascriptInterface class: `class WebViewJavascriptInterface(private val context: Context) { @JavascriptInterface fun postMessage(message: String?): String { Timber.v("some vibrate func [$message]") return message ?: "" }

@JavascriptInterface
fun vibrate() {
    Timber.v("some vibrate func [2]")
}

@JavascriptInterface
fun postMessage(): String {
    Timber.v("some vibrate func")
    return "some vibrate func"
}

}` this is javascript code

`function postNativeMessage(string, closure = null) { if (!isMobileApp) { return; }

if (window.appInterface !== undefined) {
    window.appInterface.postMessage(string);
}

if (window.myOwnJSHandler !== undefined) {
    window.myOwnJSHandler.receiveMessageFromJS(string);
}

if (window.Android !== undefined) {
    window.Android.postMessage(string);
}

if (window.webkit !== undefined) {
    window.webkit.messageHandlers.nativeApp.postMessage(string);
}

if (closure !== null) {
    closure();
}

}`

WebViewJavascriptInterface doesn't call any method from js code, what I should to do?