How to open app links in flutter webview?

半城伤御伤魂 提交于 2021-02-05 20:33:15

问题


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?


回答1:


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



回答2:


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




回答3:


You can use web plugin like

 @override
 Widget build(BuildContext context) {
   String url = widget.url;
   return Scaffold(
    body: Center(
    child : WebviewScaffold(
      url: "https://google.com",
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      withZoom: true,
      withLocalStorage: true,
     )
   ),
 );
}


来源:https://stackoverflow.com/questions/51833766/how-to-open-app-links-in-flutter-webview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!