Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open app links in flutter webview?

Tags:

flutter

In Flutter, I use the flutter webview plugin to launch a url like:

flutterWebviewPlugin.launch(url)

or

WebviewScaffold(
  url: url,
  appBar: new AppBar(title: Text(title), actions: [
    new IconButton(
      icon: const Icon(Icons.share),
      onPressed: () => Share.share(url),
    )
  ]),
  withZoom: true,
  withLocalStorage: true,
  withJavascript: true,
);

However, if any links inside the opened web page is an app link, like: fb://profile, I will get net::ERR_UNKNOWN_URL_SCHEME.

In android, I found the solution is to override shouldOverrideUrlLoading as mentioned in here, but what should I do in flutter?

like image 446
Herman Avatar asked Aug 14 '18 04:08

Herman


People also ask

How do I open URL in WebView Flutter?

Android-only settings: forceWebView – If set to null or false , the URL is opened in the device's default browser; otherwise, the URL is launched in a WebView. enableJavaScript – If set to true , JavaScript is enabled in WebView. enableDomStorage – When the value is set to true , WebView enables DOM storage.

How do you link a URL in Flutter?

linkify Flutter plugin can turn text URL and email to a clickable inline text. First, add flutter_linkify plugin to your project. Next import to the file which you are going to implement the code. print("Linkify link = ${link.


2 Answers

You can use webview_flutter in pub.dev Packages

WebView(
        initialUrl: 'https://my.url.com',
        javascriptMode: JavascriptMode.unrestricted,
        navigationDelegate: (NavigationRequest request)
        {
          if (request.url.startsWith('https://my.redirect.url.com'))
          {
            print('blocking navigation to $request}');
            _launchURL('https://my.redirect.url.com');
            return NavigationDecision.prevent;
          }

          print('allowing navigation to $request');
          return NavigationDecision.navigate;
        },
      )

And you can launch url with url_launcher in pub.dev Packages

_launchURL(String url) async {
if (await canLaunch(url)) {
  await launch(url);
} else {
  throw 'Could not launch $url';
}}
like image 87
Álvaro Agüero Avatar answered Oct 21 '22 23:10

Álvaro Agüero


Looks like you can achieve what you need using this plugin : https://pub.dartlang.org/packages/flutter_web_view

Listen for your redirects:

  flutterWebView.listenForRedirect("fb://profile", true);

Get the value using :

flutterWebView.onRedirect.listen((url) {
   flutterWebView.dismiss();
    //now you have the url
 });

After you have the url you can use this package https://pub.dartlang.org/packages/url_launcher

like image 28
diegoveloper Avatar answered Oct 21 '22 22:10

diegoveloper