使用自定义的javascriptInterfaceAdapter调整Webview或使用Webview控制器调用JS [英] Flutter webview with custom javascriptInterfaceAdapter or invoke JS using webview controller

查看:36
本文介绍了使用自定义的javascriptInterfaceAdapter调整Webview或使用Webview控制器调用JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个旧的Java项目,它在Andorid Webview中使用addJava脚本接口执行

mWebView.addJavascriptInterface(new JavascriptAdapter(), "AndroidFunctions");
JavascriptAdapter类中有几个@JavascriptInterface函数。作为示例

class JavascriptAdapter {
    @JavascriptInterface
    getMacAddress(){
        DeviceInfo deviceInfo = new DeviceInfo(activity);
        return deviceInfo.getMacAddress();
  }
}

现在,在Android应用程序中打开Webview后,使用JS可以这样访问该功能-

var strMacAddress = AndroidFunctions.getMACAddress();

现在我想在扑翼上实现这一点。为此,我使用了AppWebview插件中的Ffltter。

另外,我也尝试过fltter_webview、WKWebview。

推荐答案

请查看thisInAppWebView文档。

JavScriptInterface通常用于从Webview调用本机方法。我假设网页调用了该方法,因此,您必须返回文档中显示的Mac地址。但您需要通过将此示例(在文档中提供)添加到您的网页源代码中来更改调用Ffltter方法的方式。

<script>
        window.addEventListener("flutterInAppWebViewPlatformReady", function(event) {
            window.flutter_inappwebview.callHandler('handlerFoo')
              .then(function(result) {
                // print to the console the data coming
                // from the Flutter side.
                console.log(JSON.stringify(result));
                
                window.flutter_inappwebview
                  .callHandler('handlerFooWithArgs', 1, true, ['bar', 5], {foo: 'baz'}, result);
            });
        });
    </script>

这篇关于使用自定义的javascriptInterfaceAdapter调整Webview或使用Webview控制器调用JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆