Flutter webview with custom javascriptInterfaceAdapter or invoke JS using webview controller(使用自定义的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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用自定义的javascriptInterfaceAdapter调整Webview或使用Webview控制器调用JS
基础教程推荐
- CORS:当凭据标志为真时,无法在 Access-Control-Allow-Origin 中使用通配符 2022-01-01
- 即使每次插入第一个输入的值不同,第二个输入仍显示相同的输入值 2022-01-01
- 从快速中间件中排除路由 2022-01-01
- 带角度的选项卡:仅使用 $http 在单击时加载选项卡 2022-01-01
- HTML5 画布调整为父级 2022-01-01
- 逻辑运算符 ||在 javascript 中,0 代表 Boolean false? 2022-01-01
- 最佳动态 JavaScript/JQuery 网格 2022-01-01
- 在 Javascript 中使用 Fetch API 上传文件并显示进度 2022-01-01
- 使用 jQuery 在悬停时交换 DIV 类 2022-01-01
- 当木偶师打开Chrome时,不能使用Chrome扩展 2022-01-01
