简体   繁体   中英

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?

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

In Swift you can use this code:

            if UIApplication.sharedApplication().canOpenURL(NSURL(string: "fb-messenger-api://")!) {
                let content = FBSDKShareLinkContent()
                content.contentURL = NSURL(string: url)
                content.contentTitle = "your awesome title"

                FBSDKMessageDialog.showWithContent(content, delegate: self)
            } else {
                UIApplication.sharedApplication().openURL(NSURL(string: "https://itunes.apple.com/pl/app/messenger/id454638411?mt=8")!)
            }

It's show Messenger window with content.

Not an answer but as of Today 15 April 2021, MessageDialog or FBSDKMessageDialog is deprecated Here is the FB Response

Share to Messenger SDK that allows people to share links and media from apps to Messenger will no longer be supported. Businesses and developers might need to make modifications to their app to trigger native OS sharing. People will be able to share content to Messenger using the native sharing features that is built into their devices.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM