How to Integrate the iOS Facebook SDK in Swift 5

The iOS Facebook SDK is a useful library in most iOS projects allowing you to closely integrate your iOS app with Facebook

Josh Gare
Josh Gare
Feb 25, 2018 · 4 min read
The Facebook app for iOS

Integrating the iOS Facebook SDK, to name just a few features, will give your app access to user profiles, provide login with Facebook, and share content on the ubiquitous social network.

Some may question the need for this piece, but I wanted to write about this after implementing Facebook for iOS in a recent project. When doing so, I came up against a few hurdles that others may also hinder others. Facebook provides some excellent documentation when integrating their SDK in Objective-C , unfortunately, the same cannot be said for Swift (at the time of writing).

Hopefully, the following steps will provide you a set of instructions that are somewhat more clear than what Facebook currently provides.


Step 1: Set Up Your Facebook App

First, you will need to open the Facebook Apps Dashboard and either create a new app or navigate to an existing app.

You then need to select your app’s settings, the basic settings will suffice for this example. Once you are here, you will be able to add an iOS platform to your Facebook app.

The Facebook Apps Dashboard after adding an iOS platform to your app’s settings

At this point, you will need to provide Facebook with your app’s bundle identifier. You will have set this when you first set up your project in Xcode. It can be found in your Info.plist and will be similar to the following form:

com.yourcompany.YourProject

Make sure you have added this to the Bundle ID field on the Facebook Apps Dashboard and save your app.


Step 2: Install the iOS Facebook SDK using CocoaPods

There are several ways you can install the iOS Facebook SDK but for ease, I will only cover CocoaPods. If you’re new to CocoaPods you can find out more here.

CocoaPods is a dependency manager that lets you install the iOS Facebook SDK in a simple and efficient way. It will also make sure that your app is running the latest version of the iOS Facebook SDK so that you don’t have to worry about it.

Add the following into your Podfile:

use_frameworks!

pod 'FacebookCore'
pod 'FacebookLogin'
pod 'FacebookShare'

From the command line in your project’s root directory run:

pod install

After this process has completed your project will contain all of the necessary files and settings to continue integrating your project with the iOS Facebook SDK.

On a side note, if you ever want to update to the latest version of the iOS Facebook SDK you can do so with the following command:

pod update

Step 3: Configure Your Info.plist

You’ll need to navigate to your app’s Info.plist file and view it as source code. If you right click the file and select Open As > Source Code. The Info.plist file will be displayed as XML and you will need to add the following snippet before the final </dict> tag.

Finally, you will need to replace {your-app-id} and {your-app-name} with your App ID and App Name from the Facebook Apps Dashboard.


Step 4: Hook Up Your App Delegate

First of all, you will need to import the Facebook SDK into your AppDelegate:

import FBSDKCoreKit

Initiate the iOS Facebook SDK in the didFinishLaunchingWithOptions method with the following line:

ApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)

Ensure your app can handle the Facebook authorization URLs by adding the following method to your app’s delegate:


Step 5 (Recommended): Track App Events

The final step is to let Facebook know when a user has launched your app. This is useful if you plan to advertise your app through Facebook or if you want to use their analytics to track your app’s metrics.

Add the following line to your applicationDidBecomeActive method with your app’s delegate:

AppEventsLogger.activate(application)

That should cover everything you need to set up the initial integration of your app and the iOS Facebook SDK in Swift 5. You will now be able to utilize all of the features packed into Facebook’s SDK.


If this article has been helpful to you, please help me out and smash that clap button below 👏.

If you need more help, would like to contact me or you’re just interested in what I’m up to, you can follow on Twitter @joshgare.

Better Programming

Advice for programmers.

Josh Gare

Written by

Josh Gare

Co-Founder at Venn Apps — Full Stack Developer in London — Developing apps since 2008

Better Programming

Advice for programmers.