Andrei Herford Andrei Herford - 1 year ago 89
iOS Question

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

I would like to link a Facebook page from within my iOS app. If the Facebook App is installed on the same device, the page should be displayed there, or otherwise in 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];


This should work in theory: If I enter the facebookAppURL directly in Safari, or skip the
canOpenURL
check, I am asked if the link should be opened in Facebook. After clicking OK, the Facebook App comes up and shows the page.

However calling
[app canOpenURL: facebookAppURL]
allays returns
false
.

The same method works fine with Twitter URLs, e.g.
twitter://user?screen_name=...
: The check only returns
true
, if the Twitter App is installed and can handle the URL. Why does this fails for Facebook links?

Answer Source

Did you add this to the whitelist? From iOS9, for security reasons, you need to make whitelist all custom URL schemes. You must whitelist the URL's that your app will call out to using the LSApplicationQueriesSchemes key in your Info.plist.

This is from docs:

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.

This is how you create whitelist

<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>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download