如何在flutter webview中打开应用程序链接?

Her*_*man 10 flutter

在 Flutter 中,我使用 flutter webview 插件来启动一个 url,如:

flutterWebviewPlugin.launch(url)
Run Code Online (Sandbox Code Playgroud)

或者

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,
);
Run Code Online (Sandbox Code Playgroud)

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

在android中,我发现解决方案是覆盖这里提到的shouldOverrideUrlLoading ,但是在flutter中我应该怎么做?

Álv*_*ero 13

你可以在 pub.dev Packages 中使用webview_flutter

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;
        },
      )
Run Code Online (Sandbox Code Playgroud)

您可以在 pub.dev Packages 中使用url_launcher启动 url

_launchURL(String url) async {
if (await canLaunch(url)) {
  await launch(url);
} else {
  throw 'Could not launch $url';
}}
Run Code Online (Sandbox Code Playgroud)


die*_*per 3

看起来您可以使用此插件实现您需要的功能: https://pub.dartlang.org/packages/flutter_web_view

监听您的重定向:

  flutterWebView.listenForRedirect("fb://profile", true);
Run Code Online (Sandbox Code Playgroud)

使用以下方法获取值:

flutterWebView.onRedirect.listen((url) {
   flutterWebView.dismiss();
    //now you have the url
 });
Run Code Online (Sandbox Code Playgroud)

获得 url 后,您可以使用此包https://pub.dartlang.org/packages/url_launcher