【Flutter】 webview_flutter避坑
webview_flutter
webview_flutter没有SSL Error接口,也就是说等你的网页出现SSL 错误的时候这个插件无法捕捉处理,除非你改它的源码。
下面这段是webview_flutter官网的例子,它有onHttpError、onWebResourceError、但没有任何捕捉 SSL 错误的选项,我曾经不信邪找了很久。
controller = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setBackgroundColor(const Color(0x00000000))
..setNavigationDelegate(
NavigationDelegate(
onProgress: (int progress) {
// Update loading bar.
},
onPageStarted: (String url) {},
onPageFinished: (String url) {},
onHttpError: (HttpResponseError error) {},
onWebResourceError: (WebResourceError error) {},
onNavigationRequest: (NavigationRequest request) {
if (request.url.startsWith('https://www.youtube.com/')) {
return NavigationDecision.prevent;
}
return NavigationDecision.navigate;
},
),
)
..loadRequest(Uri.parse('https://flutter.dev'));
通过日志发现可以取巧提醒SSL Error , SSL Error不会走onPageStarted,可以在这里加flag,然后在pageFinished的时候做判断即可。
flutter_inappwebview
但是另一个webview的插件flutter_inappwebview可以捕捉 ,这个插件感觉接口更多一点。
SSL Error接口使用onReceivedServerTrustAuthRequest捕捉。
InAppWebView(
key: webViewKey,
initialUrlRequest: URLRequest(url: WebUri("https://www.baidu.com/?tn=02003390_20_hao_pg")),
initialSettings: settings,
pullToRefreshController: pullToRefreshController,
onWebViewCreated: (controller) {
webViewController = controller;
},
onReceivedServerTrustAuthRequest: (controller, challenge) async {
//解决 handshake failed问题
print("onReceivedServerTrustAuthRequest $challenge");
return ServerTrustAuthResponse(
action: ServerTrustAuthResponseAction.PROCEED);
},
onLoadStart: (controller, url) {
setState(() {
this.url = url.toString();
urlController.text = this.url;
});
},
onPermissionRequest: (controller, request) async {
return PermissionResponse(
resources: request.resources,
action: PermissionResponseAction.GRANT);
},
shouldOverrideUrlLoading:
(controller, navigationAction) async {
var uri = navigationAction.request.url!;
return NavigationActionPolicy.ALLOW;
},
onLoadStop: (controller, url) async {
pullToRefreshController?.endRefreshing();
setState(() {
this.url = url.toString();
urlController.text = this.url;
});
},
onReceivedError: (controller, request, error) {
pullToRefreshController?.endRefreshing();
},
onProgressChanged: (controller, progress) {
if (progress == 100) {
pullToRefreshController?.endRefreshing();
}
setState(() {
this.progress = progress / 100;
urlController.text = url;
});
},
onUpdateVisitedHistory: (controller, url, androidIsReload) {
setState(() {
this.url = url.toString();
urlController.text = this.url;
});
},
onConsoleMessage: (controller, consoleMessage) {
if (kDebugMode) {
print(consoleMessage);
}
},
),
原文地址:https://blog.csdn.net/lxjlxj2333/article/details/140290999
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!