flutterwebview_flutter与JavaScript交互
使⽤webview_flutter iOS需要在xcode⼯程中info.plist⽂件添加如果字典
<key>bedded_views_preview</key>
<string>YES</string>
webview_flutter与交互
1.通过拦截url的⽅式
navigationDelegate: (NavigationRequest navigation) {
String url = navigation.url;
if (ains("micrpayclient://")) {
//之前定义的micrpayclient保持不变
String lStrig = 'micrpayclient://url=';
int index = url.indexOf(lStrig);
String subString = url.substring(index + lStrig.length);
try {
var dUrl = Uri.decodeComponent(subString);
launch(dUrl);
} catch (error) {
print(error);
}
return NavigationDecision.prevent;
}
return NavigationDecision.navigate;
},
2.通过JavascriptChannel来实现
WebView(
initialUrl: widget.url,
userAgent:
"Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Mobile Safari/537.36",      navigationDelegate: listenWebview,
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (vc) {
_controller = vc;
print('webviewUrl = ${widget.url}');
},
onPageFinished: (String value) {
print('webviewUrl = $value');
_handleNaviData();
},
javascriptChannels: [
JavascriptChannel(
name: 'tudouApp',//handleName
onMessageReceived: (JavascriptMessage message) {
ssage);
//接收到js返回的数据
//⾃定义处理
}),
JavascriptChannel(
name: 'JSHandle',//handleName
onMessageReceived: (JavascriptMessage message) {flutter支持鸿蒙吗
ssage);
//接收到js返回的数据
//收到js返回并作出应答
String callbackname = ssage;
String data = "收到消息调⽤了";
String script = "$callbackname($data)";
_controller.evaluateJavascript(script);
}),
].toSet(),
)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。