Frontend Engineer
jsでwindowに関数を定義
window.hoge = function(arg) { return `arg: ${arg}` }
Kotlinから呼び出す。戻り値があれば第二引数のコールバックで受け取る
fun sample () { val arg = "example string" webView?.evaluateJavascript("window.sampleEvent(${arg})", ValueCallback<String> { result -> println(result) }) }
@JavascriptInterfaceをつけた関数を持つクラスを定義
class MyJavaScriptInterface(private val context: Context) { @JavascriptInterface fun showToast(string: String) { Toast.makeText(context, string, Toast.LENGTH_SHORT).show() } }
Nativeという名前でWebView内のJSに注入
webView.addJavascriptInterface(MyJavaScriptInterface(this), "Native")
JSから呼び出し
Native.showToast('PushMe clicked!')
jsでwindowに関数を定義
window.hoge = function(arg) { return `arg: ${arg}` }
Kotlinから呼び出す。戻り値があれば第二引数のコールバックで受け取る
fun sample () { val arg = "example string" webView?.evaluateJavascript("window.sampleEvent(${arg})", ValueCallback<String> { result -> println(result) }) }
@JavascriptInterfaceをつけた関数を持つクラスを定義
class MyJavaScriptInterface(private val context: Context) { @JavascriptInterface fun showToast(string: String) { Toast.makeText(context, string, Toast.LENGTH_SHORT).show() } }
Nativeという名前でWebView内のJSに注入
webView.addJavascriptInterface(MyJavaScriptInterface(this), "Native")
JSから呼び出し
Native.showToast('PushMe clicked!')
Frontend Engineer