flutter 采用的插件是flutter_inappwebview
1 APP调用js的函数
//h5端
window.appCallJs = function(args){
console.log(args)
}
//app端
webView.injectScriptCode("window.appCallJs('hello world')");
2 js调用APP的方法
// 代码
window.flutter_inappbrowser.callHandler('authSuccess','success',1234).then(function(result) {
console.log(result, typeof result);
});
//app代码
webView.addJavaScriptHandler('authSuccess', (args) {
print("收到来自web的消息");
});
//这样写,App端没有问题。但是web端存在问题,“{message: Uncaught TypeError: Cannot read property 'callHandler' of undefined, messageLevel: 3}”。
需要对callHandler进行处理,即:
<script>
var message = "success";
if (window.flutter_inappwebview.callHandler) {
window.flutter_inappwebview.callHandler('authSuccess', message);
} else {
window.flutter_inappwebview._callHandler('authSuccess', setTimeout(function(){}), JSON.stringify([message]));
}
</script>