Add event handler for link
In a project, we want to use the webview as a rich formatted text label. This label could contains links to some urls but tapping on it must not change the url of the webview as it is not meant to show other thing than the initial text.
It would be great if we could catch the event of tapping a url link in the webview so that we can extract the intent and open the browser application or do any other appropriate action.
For Android, this could be done using shouldOverrideUrlLoading in the WebViewClient.
On iOS, this could be done using webView:decidePolicyForNavigationAction:decisionHandler: of the WKNavigationDelegate of the webview.
for now i'm using url_launcher plugin to open url and goBack(). But this method still shows the ugly ERR_UNKNOWN_URL_SCHEME screen as this listener is invoked only after page is fully loaded.
flutterWebviewPlugin.onUrlChanged.listen((String url) {
if(url.contains("whatsapp://")) {
flutterWebviewPlugin.goBack();
_launchURL(url);
}
});
for now i'm using
url_launcherplugin to open url andgoBack(). But this method still shows the ugly ERR_UNKNOWN_URL_SCHEME screen as this listener is invoked only after page is fully loaded.flutterWebviewPlugin.onUrlChanged.listen((String url) { if(url.contains("whatsapp://")) { flutterWebviewPlugin.goBack(); _launchURL(url); } });
You don't need to go back. You can just launch the url and abort the load, so you stay in the same page. I think this can help you: https://github.com/fluttercommunity/flutter_webview_plugin/pull/292
You don't need to go back.
I have a href to https://wa.me/${this.user.phone} in a page which loads whatsapp intermediate page which again loads whatsapp:// intent. intent throws a ERR_UNKNOWN_URL_SCHEME page way before onUrlChanged listener callback is invoked. it appears that callback is called only after webview loads the page (or not sure if it might be happening async). so I had to goBack() to cover it up which looks ugly though. only if we had a beforeUrlChange callback life would be more better.
P.S: or I can directly match wa.me and do the same. that's right. goBack() is not needed here.
Support to abort loading specific URLs was added in 0.3.2
related issue #337
Closing this as the new webview_flutter plugin seems better in every way.