Flutter在Facebook应用程序中打开Facebook链接Android & iOS

twh00eeo  于 2023-05-19  发布在  Flutter
关注(0)|答案(6)|浏览(357)

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

qeeaahzv

qeeaahzv1#

我还使用了链接@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>

您可以从以下位置获取您的_page_id:https://findmyfbid.com/

a0zr77ik

a0zr77ik2#

我的工作职能:

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

 }
iovurdzv

iovurdzv3#

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

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

对于@Al Manum声明的Info.plist文件,方法canOpenUrl将始终返回true,即使Facebook应用程序未安装。
+1到this answer给我提示。
改为添加以下内容:

<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上测试。

3duebb1j

3duebb1j4#

我使用了这种方法:

Future<bool> openInFacebook({
    required bool isIOS,
    required String linkSection,
  }) async {
    final fbProtocolUri = isIOS
        ? Uri(
            scheme: 'fb',
            host: 'profile',
            path: '/$linkSection',
          )
        : Uri(
            scheme: 'fb',
            host: 'page',
            path: '/$linkSection',
          );

    final fallbackUri = Uri(
      scheme: 'https',
      host: 'www.facebook.com',
      path: '/$linkSection',
    );

    return _launchUrlSafe(fbProtocolUri, fallback: fallbackUri);
  }

  Future<bool> _launchUrlSafe(
    Uri url, {
    Uri? fallback,
  }) async {
    var success = false;
    try {
      success = await launchUrl(url);
    } catch (e, st) {
      success = false;
    }
    if (!success) {
      try {
        if (fallback != null) {
          success = await launchUrl(fallback);
        }
      } catch (e, st) {
        success = false;
      }
    }
    return success;
  }

建议使用launchUrl检查URI是否可以启动https://pub.dev/packages/url_launcher#checking-supported-schemes

h7appiyu

h7appiyu5#

我将Mode属性设置为externalApplication,然后它工作正常

Future<void> _launchUrl(String url) async {
  try {
    if (!await launchUrl(Uri.parse(url), mode: LaunchMode.externalApplication)) {
      throw Exception('Could not launch');
    }
  } catch (e) {
    Fluttertoast.showToast(msg: "$e");
  }
}
mgdq6dx1

mgdq6dx16#

您可以打开为web

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

相关问题