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

The Complete iOS 11 & Swift Developer Course - Build 20 Apps

Use Xcode 9 & Swift 4 to make real apps like Uber and Instagram, with CoreML & ARKit. Includes AWS Credit and much more. Preview this video course. icon

How to Make a Freaking iPhone App - iOS 11 and Swift 4

iPhone App Development from scratch. Learn how to make iOS apps using Xcode 9. The Basics Include Pokemon Go & Snapchat. Learn to build iOS Apps with Xcode 9 and Swift 4. How to Make a Freaking iPhone App - iOS 11 and Swift 4 icon

iOS 11 and Xcode 9 - Complete Swift 4 & Objective-C Course

A Complete iOS 11 and Xcode 9 Course with Swift 4 & Objective-C. Preview this video course. iOS 11 and Xcode 9 - Complete Swift 4 & Objective-C Course icon