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)") }
source share