【问题标题】:Open social media apps from flutter app using url_launcher使用 url_launcher 从 flutter 应用打开社交媒体应用
【发布时间】:2022-12-29 19:09:34
【问题描述】:

我想使用配置文件/业务 URL 从我的 flutter 应用程序重定向到社交媒体平台应用程序。我正在使用 url_launcher dart 包。我能够找到重定向到 Facebook 个人资料的端点,但不知道如何打开特定的 Instagram 和 Twitter 页面。

_launchFacebook(String? facebook) async {
    final url = 'fb://facewebmodal/f?href=https://' + facebook!;
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }

  _launchTwitter(String? twitter) async {
    final url = 'tw://' + twitter!;
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }

  _launchInstagram(String? instagram) async {
    final url = 'in://' + instagram!;
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    Facebook 在这里有点奇怪,要求一个特定的协议fb://facewebmodal/f?href=$url

    通常,应用程序会注册其网站的 URL 以进行深度链接,这意味着如果您没有安装相应的应用程序,它们将在您的浏览器中打开,如果您安装了相应的应用程序,它们将直接在它们的应用程序中打开。

    这是我通常使用的实现,它绝对适用于 Facebook、Twitter、Instagram 和 LinkedIn。我包括一个针对 Android 的特殊检查,这样如果您没有安装 Facebook,它会在浏览器中正确打开链接。

      @override
      Future<void> launchUrl(String url) async {
        final _canLaunch = await canLaunch(url);
        if (kIsWeb) {
          if (_canLaunch) {
            await launch(url);
          } else {
            throw "Could not launch $url";
          }
          return;
        }
        if (TargetPlatform.android) {
          if (url.startsWith("https://www.facebook.com/")) {
            final url2 = "fb://facewebmodal/f?href=$url";
            final intent2 = AndroidIntent(action: "action_view", data: url2);
            final canWork = await intent2.canResolveActivity();
            if (canWork) return intent2.launch();
          }
          final intent = AndroidIntent(action: "action_view", data: url);
          return intent.launch();
        } else {
          if (_canLaunch) {
            await launch(url, forceSafariVC: false);
          } else {
            throw "Could not launch $url";
          }
        }
      }
    

    您可以毫无问题地打开https://instagram.com/usernamehttps://twitter.com/username 格式的链接。

    希望能帮助到你 !

    【讨论】:

      【解决方案2】:

      一个简单的方法来做到这一点:

      Future<void> _launchSocialMediaAppIfInstalled({
        String url,
      }) async {
        try {
          bool launched = await launch(url, forceSafariVC: false); // Launch the app if installed!
      
          if (!launched) {
            launch(url); // Launch web view in app is not installed!
          }
        } catch (e) {
          launch(url); // Launch web view in app is not installed!
        }
      }
      

      然后简单地这样称呼它:

      _launchSocialMediaAppIfInstalled(
        url: 'https://www.instagram.com/avey.pal/', //Instagram
      );
      
      _launchSocialMediaAppIfInstalled(
        url: 'https://www.facebook.com/avey.pal/', // Facebook
      );
      
      _launchSocialMediaAppIfInstalled(
        url: 'https://twitter.com/avey_pal', // Twitter
      );
      
      _launchSocialMediaAppIfInstalled(
        url: 'https://www.linkedin.com/company/avey-ai/', // Linkedin
      );
      
      ...
      

      不要忘记用您的示例页面替换示例页面 ;) 就是这样!

      【讨论】:

        猜你喜欢
        • 2022-09-29
        • 2021-09-02
        • 2023-01-15
        • 2021-10-07
        • 1970-01-01
        • 1970-01-01
        • 2021-07-19
        • 2011-01-24
        • 1970-01-01
        相关资源
        最近更新 更多