-->

Check if user has Facebook Messenger installed iOS

2019-02-09 19:15发布

问题:

Facebook has deprecated the method [FBSDKMessengerSharer messengerPlatformCapabilities] that is used to check if the user has Messenger app installed. In the warning message, it says:

messengerPlatformCapabilities is deprecated: This is deprecated as of iOS 9. If you use this, you must configure your plist as described in https://developers.facebook.com/docs/ios/ios9

I would like to remove this method, but haven't found any other option to replace this code (that makes a button disabled if user hasn't Messenger app installed):

if (![FBSDKMessengerSharer messengerPlatformCapabilities]) {
    [self.inviteFriendsButton setEnabled:NO];
    [self.inviteFriendsButton setAlpha:0.5f];
}

Is there any other method? Or, as new iOS requirements I should avoid using this if? Thank you in advance.

回答1:

You will want to use canOpenURL to see if the Custom URL Scheme fb-messenger:// can be opened. canOpenURL returns a BOOL value indicating whether or not the URL’s scheme can be handled by some app installed on the device. If canOpenURL returns YES then the application is present on the device.

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"fb-messenger://"]]) {
    // Installed
    [self.inviteFriendsButton setEnabled:YES];
    [self.inviteFriendsButton setAlpha:1.0];
}
else {
    // NOT Installed
    [self.inviteFriendsButton setEnabled:NO];
    [self.inviteFriendsButton setAlpha:0.5];
}

Also, starting at iOS 9 you must include LSApplicationQueriesSchemes in your info.plist.



回答2:

Since the release of the Facebook SDK v4.6.0 they use fb-messenger-api as their URL scheme.

Swift 2.3

if UIApplication.sharedApplication().canOpenURL(NSURL(string: "fb-messenger-api://")!) {
    // Installed
} else {
    // Not installed
}

Source: https://developers.facebook.com/docs/ios/ios9



回答3:

For those using Swift 3, use this:

UIApplication.shared.canOpenURL(URL(string: "fb-messenger-api://")!)


回答4:

In my case I needed to know whether to show a button users could press to share content on messenger. This worked for my case, and it also checks whether the messenger app is installed.

-(BOOL) canShareViaMessenger {
    [[[FBSDKMessageDialog alloc] init] canShow]
}