使用webview_flutter iOS需要在xcode工程中info.plist文件添加如果字典
<key>io.flutter.embedded_views_preview</key>
<string>YES</string>
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返回的数据
//收到js返回并作出应答
String callbackname = message.message;
String data = "收到消息调用了";
String script = "$callbackname($data)";
_controller.evaluateJavascript(script);
}),
].toSet(),
)