flutter_webview_plugin icon indicating copy to clipboard operation
flutter_webview_plugin copied to clipboard

Add event handler for link

Open ndusart opened this issue 7 years ago • 6 comments

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.

ndusart avatar Mar 27 '19 09:03 ndusart

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);
  }
});

5hanth avatar Apr 10 '19 08:04 5hanth

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);
  }
});

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

MilloshFey avatar Apr 11 '19 12:04 MilloshFey

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.

5hanth avatar Apr 11 '19 12:04 5hanth

Support to abort loading specific URLs was added in 0.3.2

charafau avatar Apr 11 '19 14:04 charafau

related issue #337

5hanth avatar Apr 13 '19 04:04 5hanth

Closing this as the new webview_flutter plugin seems better in every way.

ndusart avatar May 09 '19 09:05 ndusart