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

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.

Related

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
}

Facebook SDK share on iOS returns share was cancelled

I'm sharing to Facebook with FB share dialog like this:
guard let localIdentifier = localIdentifier else {return}
let assetURL = "assets-library://asset/asset.MOV?id=" + localIdentifier + "&ext=MOV"
let video : FBSDKShareVideo = FBSDKShareVideo()
video.videoURL = URL(string:assetURL)
let content : FBSDKShareVideoContent = FBSDKShareVideoContent()
content.video = video
let shareDialog = FBSDKShareDialog()
shareDialog.shareContent = content
shareDialog.delegate = self
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0 , execute: {
shareDialog.show()
})
I check that the url is valid. This is a local MOV file and I copy it first to the camera roll. Then I retrieve it using PHManager and trying to share with FBSDK share dialog.
The FB sharing dialog with the movie appears and I press post. The FB seems processes the file and I get a V - that probably the video was shared.Afterwards I get the callback the share was cancelled. Did somebody experience the same? What might be the problem?
Thanks in advance.
What version of the FacebookSDK are you using? I had this problem too and it was because the version that I was using was not compatible with iOS 11 yet. I think that from v 4.27 it's compatible, I was using v4.23. Hope it helps.

Using Branch.io generated link with Facebook share dialog

I went through Branch.io's documentation a lot over the past few days and I can't seem to pinpoint the problem. I got the generated link to work properly by setting up the BranchUniversalObject as follows:
let branchUniversalObject: BranchUniversalObject = BranchUniversalObject(canonicalIdentifier: "ios_share_user")
branchUniversalObject.title = "My Title"
branchUniversalObject.contentDescription = "My Description"
branchUniversalObject.imageUrl = "www.example.com/image.jpg"
branchUniversalObject.addMetadataKey("id", value: self.userId)
branchUniversalObject.addMetadataKey("type", value: self.userType)
let linkProperties = BranchLinkProperties()
linkProperties.channel = "Facebook"
linkProperties.feature = "Share"
branchUniversalObject.getShortUrl(with: linkProperties, andCallback: { (shareURL, error) in
if error != nil {
print(error!)
return
}
// using shareURL here
})
The generated URL works perfectly fine, but only when launched from Notes or Messages apps. Safari always redirects me to the AppStore and so does the Facebook app when I try sharing the URL like this:
let content = FBSDKShareLinkContent()
content.contentTitle = "My Title"
content.contentURL = shareURL // generated above
content.contentDescription = "My Description"
content.imageURL = "www.example.com/image.jpg"
let dialog = FBSDKShareDialog()
dialog.fromViewController = self
dialog.delegate = self
dialog.shareContent = content
if dialog.canShow() {
dialog.show()
}
That shows up the dialog and shares the content as intended, but the link always redirects me to the AppStore just like in Safari.
Here's a screenshot of the Dashboard:
I also have applinks:myLinkDomain.app.link and applinks:myLinkDomain-alternate.app.link in the Associated Domains, branch_app_domain = myLinkDomain.app.link along with branch_key = ["live": "key_live_myKey"] in Info.plist, and AppDelegate is properly set.
What am I missing to get the app to open from Safari and Facebook instead of getting redirected to the AppStore?
I'm deploying the app for iOS 9.3+
Thanks!
I came across this link and it shows that the problem isn't with Branch.io SDK's setup by any means, but a limitation imposed by Apple. The only workaround is using Branch's Deepviews, which unfortunately I can't use in my case.
Hope Branch.io figure out a way around that! ...And possibly update their documentation to clearly state that some app browsers are not supported

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.

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.

Resources