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() {
        super.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)
        self.view.addSubview(inviteButton)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func inviteButtonTapped()
    {
        print("Invite button tapped")
        
        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
            inviteContent.appInvitePreviewImageURL = previewImageUrl
            
            inviteDialog.content = inviteContent
            inviteDialog.delegate = self
            inviteDialog.show()
        }
    }

    
    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.


Learn iOS Development with these Video Courses


How to Make a Freaking iPhone App - iOS 10 and Swift 3
icon icon


iOS 10 & Swift 3: From Beginner to Paid Professional
icon


The Complete iOS 10 Developer Course - Build 21 Apps
icon


Swift 3 - Learn to Code with Apple's New Language
icon icon


Learn How to Build Mobile Apps for iOS with Swift, PHP and MySQL
icon icon

Follow me on one of your favourite social networks to learn about new video tutorials and code examples:

Twitter: @SwiftVideoBlog
Google Plus: https://plus.google.com/+SergeyKargopolov/posts
Facebook: Swift Developer Blog on Facebook