在 Facebook 应用 android & ios 中打开 Facebook link

Flutter open facebook link in facebook app android & ios

在我的应用程序中,我存储了 Facebook url。我想在 facebook 应用程序中打开它们,而不是在浏览器中。我尝试使用 flutter_url_launcher 包 但它在默认浏览器中打开 link ... 我想要的是将 link 直接打开到 facebook 应用程序 中。 谁能提出任何解决方案或方案来帮助我摆脱这种情况?

您可以将其打开为 web

final url =
      "web://$**Your_Profile_Page**";
  if (await UrlLauncher.canLaunch(url)) {
    await UrlLauncher.launch(url,forceSafariVC: false);
  } else {
    throw 'Could not launch $url';
  }

我也使用了link @ruelluna 提到的解决方案。

根据当前版本,以下代码对我有用:

String fbProtocolUrl;
if (Platform.isIOS) {
  fbProtocolUrl = 'fb://profile/page_id';
} else {
  fbProtocolUrl = 'fb://page/page_id';
}

String fallbackUrl = 'https://www.facebook.com/page_name';

try {
  bool launched = await launch(fbProtocolUrl, forceSafariVC: false);

  if (!launched) {
    await launch(fallbackUrl, forceSafariVC: false);
  }
} catch (e) {
  await launch(fallbackUrl, forceSafariVC: false);
}

此外,确保 Info.plist 文件包含以下内容:

 <array> 
        <dict>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>fb[your_page_id]</string>
            </array>
        </dict>
    </array>

您可以 your_page_id 来自:https://findmyfbid.com/

我的工作职能:

 void launchUrl2() async{
   var url = 'fb://facewebmodal/f?href=https://www.facebook.com/al.mamun.me12';
   if (await canLaunch(url)) {
     await launch( url, universalLinksOnly: true, );
   } else { throw 'There was a problem to open the url: $url'; }

 }

我为此苦苦挣扎了几个小时,并找到了更新的解决方案。
首先,如果你添加

<array> 
        <dict>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>fb[your_page_id]</string>
            </array>
        </dict>
    </array>

对于@Al Manum 所述的 Info.plist 文件,方法 canOpenUrl 将始终 return 正确,即使未安装 Facebook 应用程序也是如此。
+1 到 给我提示。

改为添加以下内容:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>fb</string>
</array>

如果安装在设备上,以下代码将打开 Facebook 原生应用,否则将打开浏览器

Future<void> _openFacebook() async {
    String fbProtocolUrl;
    if (Platform.isIOS) {
      fbProtocolUrl = 'fb://profile/{your-page-id}';
    } else {
      fbProtocolUrl = 'fb://page/{your-page-id}';
    }

    String fallbackUrl = 'https://www.facebook.com/{your-page-uri}';

    try {
      Uri fbBundleUri = Uri.parse(fbProtocolUrl);
      var canLaunchNatively = await canLaunchUrl(fbBundleUri);

      if (canLaunchNatively) {
        launchUrl(fbBundleUri);
      } else {
        await launchUrl(Uri.parse(fallbackUrl),
            mode: LaunchMode.externalApplication);
      }
    } catch (e, st) {
      // Handle this as you prefer
    }
  }

在 iOS 15.5、Android 11 和 Flutter 3 上测试。