Invite Facebook Friends Code Example in Swift

How awesome is that when users of your mobile app can invite their friends to download and use your app? The code example on this page will help you add a “Invite friends” which can be user to invite their Facebook friends to download and use your app:

  • Download, Set up Facebook SDK for iOS and Import FBSDKCoreKit and FBSDKLoginKit
  • Add UIButton
  • Use FBSDKAppInviteDialog to present Invite Facebook Friends dialog window
  • Use FBSDKAppInviteContent to set app link url and app invite preview image URL
  • Handle appInviteDialog events to know for example if user has Canceled the window or if all went good.

Swift Code Example to Invite Facebook Friends


import UIKit

import FBSDKCoreKit
import FBSDKShareKit

class InviteFriendsViewController: UIViewController, FBSDKAppInviteDialogDelegate {

    override func viewDidLoad() {
        let inviteButton = UIButton(type: UIButtonType.System) as UIButton
        let xPostion:CGFloat = 50
        let yPostion:CGFloat = 100
        let buttonWidth:CGFloat = 150
        let buttonHeight:CGFloat = 45
        inviteButton.frame = CGRectMake(xPostion, yPostion, buttonWidth, buttonHeight)
        inviteButton.setTitle("Invite Friends", forState: UIControlState.Normal)
        inviteButton.tintColor = UIColor.blackColor()
        inviteButton.addTarget(self, action: "inviteButtonTapped", forControlEvents: UIControlEvents.TouchUpInside)

    override func didReceiveMemoryWarning() {
        // Dispose of any resources that can be recreated.
    func inviteButtonTapped()
        print("Invite button tapped")
        let inviteDialog:FBSDKAppInviteDialog = FBSDKAppInviteDialog()
            let appLinkUrl:NSURL = NSURL(string: "")!
            let previewImageUrl:NSURL = NSURL(string: "")!
            let inviteContent:FBSDKAppInviteContent = FBSDKAppInviteContent()
            inviteContent.appLinkURL = appLinkUrl
            inviteContent.appInvitePreviewImageURL = previewImageUrl
            inviteDialog.content = inviteContent
            inviteDialog.delegate = self

    func appInviteDialog(appInviteDialog: FBSDKAppInviteDialog!, didCompleteWithResults results: [NSObject : AnyObject]!) {
        let resultObject = NSDictionary(dictionary: results)
        if let didCancel = resultObject.valueForKey("completionGesture")
            if didCancel.caseInsensitiveCompare("Cancel") == NSComparisonResult.OrderedSame
                print("User Canceled invitation dialog")
    func appInviteDialog(appInviteDialog: FBSDKAppInviteDialog!, didFailWithError error: NSError!) {
        print("Error tool place in appInviteDialog \(error)")


Checkout more Swift code examples at Swift Code Examples page.

[raw_html_snippet id=”cookbookpagecoursesheader”]

Unit Testing Swift Mobile App

Apply Test-Driven Development(TDD) process to iOS mobile app development in Swift Preview this video course.