如何在Flutter WebView中打开应用程序链接? [英] How to open app links in flutter webview?

查看:104
本文介绍了如何在Flutter WebView中打开应用程序链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Flutter中,我使用flutter webview插件来启动类似以下网址:

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

flutterWebviewPlugin.launch(url)

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

但是,如果打开的网页内的任何链接是应用程序链接,例如: fb://profile ,我将得到net :: ERR_UNKNOWN_URL_SCHEME.

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

在android系统中,我发现解决方案是覆盖此处中提到的shouldOverrideUrlLoading,但是我该如何处理??

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

推荐答案

您可以在pub.dev程序包中使用 webview_flutter

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

您可以在pub.dev程序包中使用 url_launcher 启动url

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

这篇关于如何在Flutter WebView中打开应用程序链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆