canOpenURL:facebookAppURL return false,即使安装了Facebook App

canOpenURL:facebookAppURL return false, even if Facebook App is installed

我想从我的 iOS 应用程序中 link 一个 Facebook 页面。如果 Facebook 应用程序安装在同一台设备上,页面应该显示在那里,否则在 Safari 中:

NSURL* facebookURL = [NSURL URLWithString:@"https://www.facebook.com/SomePage/"];
NSURL* facebookAppURL = [NSURL URLWithString:@"fb://profile/247377102029332"];

UIApplication* app = [UIApplication sharedApplication];
if ([app canOpenURL: facebookAppURL])
    [app openURL:facebookAppURL];
else
    [app openURL:facebookURL];

这在理论上应该可行:如果我直接在 Safari 中输入 facebookAppURL,或者跳过 canOpenURL 检查,系统会询问我是否应该在 Facebook 中打开 link .单击确定后,Facebook 应用程序出现并显示该页面。

然而调用 [app canOpenURL: facebookAppURL] 缓解了 returns false

同样的方法适用于 Twitter URLs,例如twitter://user?screen_name=...: 只检查 returns true,如果安装了 Twitter App 并且可以处理 URL。为什么这对 Facebook links 失败?

你把这个加入白名单了吗?从 iOS9 开始,出于安全原因,您需要将所有自定义 URL 方案列入白名单。您必须使用 中的 LSApplicationQueriesSchemes 键将您的应用调出的 URL 列入白名单Info.plist.

这是来自文档:

If you call the “canOpenURL” method on a URL that is not in your whitelist, it will return “NO”, even if there is an app installed that has registered to handle this scheme. A “This app is not allowed to query for scheme xxx” syslog entry will appear.

If you call the “openURL” method on a URL that is not in your whitelist, it will fail silently. A “This app is not allowed to query for scheme xxx” syslog entry will appear.

这就是创建白名单的方式

<key>LSApplicationQueriesSchemes</key>
<array>
 <string>fb</string>
 <string>fbapi</string>
 <string>fbauth2</string>
 <string>fbshareextension</string>
 <string>fb-messenger-api</string>
 <string>twitter</string>
 <string>viber</string>
 <string>whatsapp</string>
</array>