Search code examples
iosobjective-cfacebookswiftfacebook-messenger

Using Facebook Messenger SDK in Swift


I've been trying to use Facebook Messenger SDK in my Swift Project. And the problem is that Facebook only shows how to use in Objective-C. I'm having trouble calling methods from the FBSDKMessengerShareKit. I've made bridging header and added FBSDKMessengerShareKit for import. The bridging header is like this

#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>
#import <FBSDKMessengerShareKit/FBSDKMessengerShareKit.h>

#ifndef myProject_Bridging_Header_h
#define myProject_Bridging_Header_h

#endif

This is how Facebook shows how to share an image in Messenger with Objective-C

if ([FBSDKMessengerSharer messengerPlatformCapabilities] & FBSDKMessengerPlatformCapabilityImage) {
    UIImage *image = [UIImage imageNamed:@"myImage];
    [FBSDKMessengerSharer shareImage:image withOptions:nil];
}

The way I'm changing it into Swift

if (FBSDKMessengerSharer.messengerPlatformCapabilities() & FBSDKMessengerPlatformCapability.Image) {
            let myImage = UIImage(named: "myImage")
            FBSDKMessengerSharer.shareImage(myImage, withOptions: nil)
}

My Swift code cannot be built and it always shows the error "Could not find an overload for '&' that accepts the supplied arguments"

I don't know what's wrong with my Swift code, Do anyone know how to use MessengerSDK in Swift?


Solution

  • here is the code you need :

    let result = FBSDKMessengerSharer.messengerPlatformCapabilities().rawValue & FBSDKMessengerPlatformCapability.Image.rawValue
        if result != 0 {
            // ok now share
            if let sharingImage = sharingImage {
                FBSDKMessengerSharer.shareImage(sharingImage, withOptions: nil)
            }
        } else {
            // not installed then open link. Note simulator doesn't open iTunes store.
            UIApplication.sharedApplication().openURL(NSURL(string: "itms://itunes.apple.com/us/app/facebook-messenger/id454638411?mt=8")!)
        }
    

    Check this for more reference : http://shoheik.hatenablog.com/entry/2015/03/28/120212