Swift Facebook Login Tutorial
In this tutorial we will be setting up a Facebook Login app using Swift. Unfortunately as Swift updates quite often the Facebook documentation on this is quite often outdated, lacking and missing steps. So we are going to ignore the Facebook setup guide for the most part, and only use it when it makes sense.
First of all we need to setup the Facebook Cocoapods libaries, if you do not have Cocoapods or are unsure as to what it is, check out our post on it here.
Setting Up Facebook Cocoapods
First of all create a new Single View application xcode project. Once you have set it up navigate to the project in your terminal and initialize CocoaPods with pod init. After this we need to add the following CocoaPod libraries to our Podfile:
pod ‘FacebookCore’
pod ‘FacebookLogin’
So our whole Podfile will look something like the following, in my case the project name is ‘FirebaseFacebook’
# Uncomment the next line to define a global platform for your project # platform :ios, '9.0' target 'FirebaseFacebook' do # Comment the next line if you're not using Swift and don't want to use dynamic frameworks use_frameworks! # Pods for FirebaseFacebook pod 'FacebookCore' pod 'FacebookLogin' target 'FirebaseFacebookTests' do inherit! :search_paths # Pods for testing end target 'FirebaseFacebookUITests' do inherit! :search_paths # Pods for testing end end
Now with our cocoapods run pod install to install our pods.
Easy right? Not so sadly…
CocoaPods does not always install the latest Facebook libarries. As a result we must run the following commands in terminaly one by one to update them, each one will take 3-5 minutes to fully update.
pod update FBSDKCoreKit
pod update FacebookCore
pod update FBSDKLoginKit
pod update Facebook Login
Now we actually have the latest Swift Facebook SDK before we get into the coding we need to setup our app in Facebook Developers.
Facebook Developer Setup
Go to https://developers.facebook.com/docs/facebook-login/ios. In Step 1 add your Bundle ID for the app, ours is ‘seemu.FirebaseFacebook’, it can be found in your project settings under general.
Next up toggle the enable Single Sign On. After this skip to Step 4. Configure Your Project. Copy the code for the info.plist file to your app as instructed, you can skip Step 4b. Build Settings, we do not need to add the -ObjC linker flag as we are using the Swift SDK.
Now we can head on to the coding
Coding Facebook Login in Swift
First of all the setup, in our AppDelegate.swift under import UIKit add:
import FacebookCore
Now in our applicationDidFinishLAucnhing with options add the Setup so yours looks as follows:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions) return true }
Then we need to add the following block to the App Delegate:
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool { return SDKApplicationDelegate.shared.application(app, open: url, options: options) }
Then head on over to our ViewController.swift, under import UIKit add the following imports:
import FacebookCore
import FacebookLogin
Then to let the user login to Facebook add the following lines to viewDidLoad to create our login button:
override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let loginButton = LoginButton(readPermissions: [.publicProfile]) loginButton.center = view.center self.view.addSubview(loginButton) }
Now we can test our Facebook Login, run your app and Login with Facebook!
You will notice once you Login with Facebook the button will change to Logout. It is able to track your logged in state to allow you to logout.
Now we have implemented the login lets check out how we can call some code when a user logs in and out. We will also check out how to get the userid which would be used to uniquely identify a given user in your app.
Facebook login delegate and userid
In our ViewController add the LoginButtonDelegate protocl as follows:
class ViewController: UIViewController, LoginButtonDelegate {
Next up modify our viewDidLoad as follows, you will notice we set the loginButton delgate as self and get the user’s access token if they are already logged in.
override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let loginButton = LoginButton(readPermissions: [.publicProfile]) loginButton.center = view.center loginButton.delegate = self self.view.addSubview(loginButton) if let accessToken = AccessToken.current { // User is already logged in with facebook print("User is already logged in") print(accessToken) } }
Then add the following two functions to our class. The first one will be automatically called once the user completed the Facebook Login, the result can be a failure, cancelled or success. In each one you will see what parameters you can get from it.
The main ones you will need are in success. It will return the Facebook permissions the user has granted to your app and their accessToken.
func loginButtonDidCompleteLogin(_ loginButton: LoginButton, result: LoginResult) { print("User logged in") switch result { case .failed(let err): print(err) case .cancelled: print("cancelled") case .success(let grantedPermissions, let declinedPermissions, let accessToken): print("success") } } func loginButtonDidLogOut(_ loginButton: LoginButton) { print("User logged out") }
AccessToken(appId: “254686711725675”, authenticationToken: “EAADnot77PmsBAMyCGViiVqeNzZBPFZCeMiVXJ6SMHdbZCNzqurdis2LhBNYUlZBAfGcoUSHz1TxFmE20yl0TNUZBhkfBFKdWrMeEiJTFIOTCMHg3PA9EhPIg1VdkaPNwVtBHgymynuzJvfUlSZAbExBZBPRwxwpeZBeHsKZC8nQZCXJkkbnHgAGaUkB8RyqUxZAzV7yPAz4hECFKTnZAhmCotpZAyVKR8ZC4ckcegZD”, userId: Optional(“10156908586856465”), refreshDate: 2019-01-29 11:43:51 +0000, expirationDate: 2019-03-30 09:31:00 +0000, grantedPermissions: Optional(Set([FacebookCore.Permission(name: “public_profile”)])), declinedPermissions: Optional(Set([])))