Using Facebook Messenger SDK in Swift
I am 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 am having trouble calling methods from the FBSDKMessengerShareKit. I concatenated the header and added the FBSDKMessengerShareKit to import. The bridge header looks 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 you how to share an image on Messenger using Objective-C
if ([FBSDKMessengerSharer messengerPlatformCapabilities] & FBSDKMessengerPlatformCapabilityImage) {
UIImage *image = [UIImage imageNamed:@"myImage];
[FBSDKMessengerSharer shareImage:image withOptions:nil];
}
How I change it to Swift
if (FBSDKMessengerSharer.messengerPlatformCapabilities() & FBSDKMessengerPlatformCapability.Image) {
let myImage = UIImage(named: "myImage")
FBSDKMessengerSharer.shareImage(myImage, withOptions: nil)
}
My Swift code cannot be generated and it always shows the error "Could not find an overload for" & "that takes the supplied arguments"
I don't know what happened to my Swift code. Does anyone know how to use MessengerSDK in Swift?
source to share
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 it out for more help: http://shoheik.hatenablog.com/entry/2015/03/28/120212
source to share
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")!)
}
Displays a Messenger window with content.
source to share