Check if user has Facebook Messenger installed iOS 9

十年热恋 提交于 2019-11-29 04:35:11

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.

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

For those using Swift 3, use this:

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

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]
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!