Swift 2.0 for Facebook invitation

I was looking for an equivalent Swift code example to invite a Facebook friend for iOS apps. But I canโ€™t find them.

I understand that there is a version of Objective-C on the Facebook page https://developers.facebook.com/docs/app-invites/ios . However, since I started with Swift, it's hard for me to translate.

Can someone point me to the source? Thanks.

+5
source share
3 answers

working code:

-In viewDidLoad:

let content = FBSDKAppInviteContent() content.appLinkURL = NSURL(string: "https://test/myapplink") content.appInvitePreviewImageURL = NSURL(string: "https://test/myapplink") // Old Way, now depreciated : //FBSDKAppInviteDialog.showFromViewController(self, withContent: content, delegate: self) //New way : FBSDKAppInviteDialog.showFromViewController(self, withContent: content, delegate: self) // Do any additional setup after loading the view. 

-In your viewController to negotiate a protocol delegate:

 extension InviteFriendsViewController: FBSDKAppInviteDialogDelegate{ func appInviteDialog(appInviteDialog: FBSDKAppInviteDialog!, didCompleteWithResults results: [NSObject : AnyObject]!) { //TODO } func appInviteDialog(appInviteDialog: FBSDKAppInviteDialog!, didFailWithError error: NSError!) { //TODO } } 
+11
source

Facebook Friend Invitation in Swift 3.0

First of all, import FBSDKCoreKit , FBSDKShareKit and add the delegate FBSDKAppInviteDialogDelegate . Then, by clicking the friendโ€™s invitation button, add the following code:

 let inviteDialog:FBSDKAppInviteDialog = FBSDKAppInviteDialog() if(inviteDialog.canShow()){ let appLinkUrl:NSURL = NSURL(string: "http://yourwebpage.com")! let previewImageUrl:NSURL = NSURL(string: "http://yourwebpage.com/preview-image.png")! let inviteContent:FBSDKAppInviteContent = FBSDKAppInviteContent() inviteContent.appLinkURL = appLinkUrl as URL! inviteContent.appInvitePreviewImageURL = previewImageUrl as URL! inviteDialog.content = inviteContent inviteDialog.delegate = self inviteDialog.show() } 

Then add the following FBSDKAppInviteDialogDelegate methods:

 func appInviteDialog (_ appInviteDialog: FBSDKAppInviteDialog!, didCompleteWithResults results: [AnyHashable : Any]!) { let resultObject = NSDictionary(dictionary: results) if let didCancel = resultObject.value(forKey: "completionGesture") { if (didCancel as AnyObject).caseInsensitiveCompare("Cancel") == ComparisonResult.orderedSame { print("User Canceled invitation dialog") } } } func appInviteDialog(_ appInviteDialog: FBSDKAppInviteDialog!, didFailWithError error: Error!) { print("Error tool place in appInviteDialog \(error)") } 
+6
source

Benobab's solution is perfect, I just want to add that in my case, trying to run FBSDKAppInviteDialog.showFromViewController on viewDidAppear worked better.

0
source

All Articles