Using Facebook Messenger SDK in Swift - ios

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.

Related

How to invite Facebook friends to my iOS application in Swift?

I am developping an iOS application using Swift 4.
I need that the user can invite his Facebook friends to use this application.
I tried this code :
func inviteFacebookFriends(){
let dialog = FBSDKAppInviteDialog()
let content:FBSDKAppInviteContent = FBSDKAppInviteContent()
content.appLinkURL = URL(string: "https://example/fbid")
content.appInvitePreviewImageURL = URL(string: "http://www.queness.com/resources/images/png/apple_ex.png")
dialog.content = content
dialog.delegate = self
do {
try dialog.validate()
}
catch {
print(error)
}
dialog.show()
}
But nothing happened, there's no dialog appears.
please help me to solve this issue.
Facebook invite is no longer allowed by Facebook SDK. There's no way to use FBSDKAppInviteDialog anymore.
With the release of the Facebook SDK version 4.28.0, App Invites is deprecated. It will be supported until February 5, 2018.
From Facebook docs
You will have to find an other way than Facebook to invite friends.

Facebook ShareDialog crashes with EXC_BAD_ACCESS if Facebook app is not installed (iOS 11.3 XCode 9.3)

Until iOS 11, I was using SLComposeViewController for sharing with Facebook, but now it's deprecated.
So I've added Facebook SDK's ShareDialog to my project.
If Facebook App is installed share process is done successfully, but if Facebook app is not installed Facebook SDK throws EXC_BAD_ACESS. You can see the screen where it crashed. Crash occurs in SDKSharingDelegateBridge.swift class.
If app not installed it should supposed to open WebView or give some error. It shouldn't crash and give me no error.
Since there is no log for the crash I only have EXC_BAD_ACCESS.
My environment:
XCode 9.3
Swift 3.3
iOS 11.3
FacebookShare (0.3.0) (from CocoaPods)
Have anyone came across to this situation? Thx already.
Below is the code snippet from the app which is copied from the offical docs. (https://developers.facebook.com/docs/swift/sharing/share-dialog)
var shareContent: LinkShareContent?
if let urlstring = urlStr, let url = URL(string: urlstring) {
shareContent = LinkShareContent(url: url, quote: initialText)
}
let shareDialog = ShareDialog(content: shareContent!)
shareDialog.mode = .native
shareDialog.failsOnInvalidData = true
shareDialog.completion = { result in
// Handle share results
}
do {
try shareDialog.show()
}
catch {
}
Until better answer I've fixed it like below.
if UIApplication.shared.canOpenURL(URL(string: "fb://")!){
shareDialog.mode = .native
}
else {
shareDialog.mode = .automatic
}

NSURL Error in XCode 8.2.1

BEGINNER ALERT! Please talk to me like I'm a 5-year-old because I am new to this platform and language. I will be grateful and unoffended.
I have the following code in my Xcode project to open a website in the native browser from a button on the app home page:
#IBAction func faceURL(_ sender: Any) {
NSURL *spotiURL = [NSURL URLWithString:#"https://www.facebook.com"];
[[UIApplication sharedApplication] openURL:faceURL options:#{} completionHandler:^(BOOL success) {
if (success){
NSLog(#"Opened url");
}
}];
}
On the line:
NSURL *spotiURL = [NSURL URLWithString:#"https://www.facebook.com"];
it's throwing the following error:
Expected "," separator Consecutive statements on a line must be separated by ";"
It recommends putting a ; between the colon and # symbol before the URL. I know some of this is deprecated with the new Xcode and Swift format, but I can't find a straightforward answer for this particular issue. Thank you in advance for your wisdom.
UPDATE
Thank you to the folks who helped with this. In case anyone else comes across this issue, here is the code that ended up working to touch the button and open a URL in the native browser:
#available(iOS 10.0, *)
#IBAction func openURL(_ sender: Any) {
let openURL = URL(string: "https://www.facebook.com")
UIApplication.shared.open(openURL!, options: [:], completionHandler: nil)
}
I also had to delete the original button and corresponding reference in the ViewController, replace the button, and do a fresh connection with ViewController to get it working. Many thanks!
It looks like you're trying to use Objective C code in a Swift method. If your project is written in Swift then this is (probably) the code you need:
#IBAction func faceURL(_ sender: Any) {
if let facebookURL = URL(string: "https://www.facebook.com")
{
UIApplication.shared.openURL(facebookURL) // the open method you were using doesn't exist in Swift
}
}
I'd suggest you work through a few tutorials to learn the basics of iOS development.
Well, you are mixing Swift (first line) and ObjC (all the other) syntax, so you have to choose one of them first of all. Your "mixed" code looks good btw.
First: you have faceURL in openURL when it should be spotiURL.
Second: you mean to convert the code you copied from SO from Obj-C to Swift which I will do for you :)
let spotiURL = URL(string: "https://www.facebook.com")
UIApplication.shared.openURL(spotiURL!)

How do I add a rate me button to an app that has not been released?

So I am about to launch an app to the App Store. My issue is that I have a rate my app please button but I do not know the right code to insert there.
My fiends tried this on their app and said it was no good:
Does anybody know how I can fix this issue?
let iTunesReviewPageLink = "http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=1073785561&pageNumber=0&sortOrdering=2&type=Purple+Software&mt=8"
// Go directly to review page on App Store
if let url = NSURL(string: iTunesReviewPageLink) {
UIApplication.sharedApplication().openURL(url)
}
The only unknown thing is the ID, right? You can see the ID of your app before it is published - once you set it up in iTunes Connect.
If your app is not yet released, you haven't got an App Store link. So that's impossible.
In order to implement this functionality when your app is released, you can use the following code:
Swift 2
let appIDString = "APP_ID" // your app ID
let reviewsURLString = "http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?pageNumber=0&sortOrdering=1&type=Purple+Software&mt=8&id=\(appIDString)"
let reviewsURL = NSURL(string: reviewsURLString)
if reviewsURL != nil && UIApplication.sharedApplication().canOpenURL(reviewsURL!) {
UIApplication.sharedApplication().openURL(reviewsURL!)
}
else {
// handle situation if reviews url cannot be opened.
}
Swift 3
let appIDString = "APP_ID" // your app ID
let reviewsURLString = "http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?pageNumber=0&sortOrdering=1&type=Purple+Software&mt=8&id=\(appIDString)"
let reviewsURL = URL(string: reviewsURLString)
if reviewsURL != nil && UIApplication.shared.canOpenURL(reviewsURL!) {
UIApplication.shared.openURL(reviewsURL!)
}
else {
// handle situation if reviews url cannot be opened.
}
EDIT:
This links works in iOS 8 and 9 and links directly to reviews page of the app in App Store application.
I am not sure about iOS 7. Probably for iOS 7 you need to use different link.

How to share Facebook page using Xamarin.iOS Facebook SDK?

I use this code in my apps to share Facebook pages:
class func shareWithDialog(viewController : UIViewController, delegate : FBSDKSharingDelegate, contentTitle: String, contentUrl: NSURL = url, contentDescription: String) {
let content = FBSDKShareLinkContent()
content.contentTitle = contentTitle
content.contentURL = contentUrl
content.contentDescription = contentDescription
let dialog = FBSDKShareDialog()
dialog.fromViewController = viewController
dialog.shareContent = content
dialog.mode = FBSDKShareDialogMode.FeedBrowser
dialog.delegate = delegate
dialog.show()
}
In the native code (above) FBSDKShareLinkContent.contentURL is set to the facebook page I want to share. I'm using Xamarin.Facebook.iOS.4.5.1.0 nuget package to add this functionality into a Xamarin app. The thing is that this SDK is similar to the native SDK but class ShareLinkContent doesn't have the property contentURL (it does have a ImageURL property).
I might be using the wrong class, but I could'n find the right class (other classes in the SDK are intended for sharing images and videos).
Thanks to Jason Steele I looked closer and see that contentUrl property was replaced by setContentURL() method. I used this method to point to the Facebook page I wanted to share and it worked.
It was a silly question after all ;)

Resources