webview_flutter与交互
1.通过拦截url的方式
navigationDelegate: (NavigationRequest navigation) {
String url = navigation.url;
if (url.contains("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) {
print(message.message);
//接收到js返回的数据
//自定义处理
}),
JavascriptChannel(
name: 'JSHandle',//handleName
onMessageReceived: (JavascriptMessage message) {
print(message.message);
//接收到js返回的数据
//自定义处理
}),
].toSet(),
)
来源:oschina
链接:https://my.oschina.net/u/4329429/blog/3220477