Facebook Login for iOS - Quickstart

When people log into your app with Facebook, they can grant permissions to your app so you can retrieve information or perform actions on Facebook on their behalf.

The following steps are for adding Facebook Login to your iOS project.

1. Login

Please log in to Facebook to create apps or register as a developer.

2. Set up Your Development Environment

Set up your development environment before using Facebook Login for iOS.

Using Swift Package Manager (SPM)
  1. In Xcode, click File > Swift Packages > Add Package Dependency.
  2. In the dialog that appears, enter the repository URL: https://github.com/facebook/facebook-ios-sdk.
  3. In Version, enter the version number of the latest Facebook SDK for iOS.
  4. Complete the prompts to select the libraries you'd like to use in your project.

3. Register and Configure Your App with Facebook

Register and configure your app so you can use Facebook Login by adding your Bundle Identifier.

You need to login to complete this step.

4. Configure Your Project

Configure the Info.plist file with an XML snippet that contains data about your app.
  1. Right-click Info.plist, and choose Open As ▸ Source Code.
  2. Copy and paste the following XML snippet into the body of your file ( <dict>...</dict>).
    <key>CFBundleURLTypes</key>
    <array>
      <dict>
      <key>CFBundleURLSchemes</key>
      <array>
        <string>fbAPP-ID</string>
      </array>
      </dict>
    </array>
    <key>FacebookAppID</key>
    <string>APP-ID</string>
    <key>FacebookClientToken</key>
    <string>CLIENT-TOKEN</string>
    <key>FacebookDisplayName</key>
    <string>APP-NAME</string>
  3. In <array><string> in the key [CFBundleURLSchemes], replace APP-ID with your App ID.
  4. In <string> in the key FacebookAppID, replace APP-ID with your App ID.
  5. In <string> in the key FacebookClientToken, replace CLIENT-TOKEN with the value found under Settings > Advanced > Client Token in your App Dashboard.
  6. In <string> in the key FacebookDisplayName, replace APP-NAME with the name of your app.
  7. To use any of the Facebook dialogs (e.g., Login, Share, App Invites, etc.) that can perform an app switch to Facebook apps, your application's Info.plist also needs to include: <dict>...</dict>).
    <key>LSApplicationQueriesSchemes</key>
    <array>
      <string>fbapi</string>
      <string>fbapi20130214</string>
      <string>fbapi20130410</string>
      <string>fbapi20130702</string>
      <string>fbapi20131010</string>
      <string>fbapi20131219</string>
      <string>fbapi20140410</string>
      <string>fbapi20140116</string>
      <string>fbapi20150313</string>
      <string>fbapi20150629</string>
      <string>fbapi20160328</string>
      <string>fbauth</string>
      <string>fb-messenger-share-api</string>
      <string>fbauth2</string>
      <string>fbshareextension</string>
    </array>

5. Connect Your App Delegate

Replace the code in AppDelegate.swift method with the following code. This code initializes the SDK when your app launches, and allows the SDK handle results from the native Facebook app when you perform a Login or Share action.
    
// AppDelegate.swift
import UIKit
import FacebookCore

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {    
    func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {          
        ApplicationDelegate.shared.application(
            application,
            didFinishLaunchingWithOptions: launchOptions
        )

        return true
    }
          
    func application(
        _ app: UIApplication,
        open url: URL,
        options: [UIApplication.OpenURLOptionsKey : Any] = [:]
    ) -> Bool {
        ApplicationDelegate.shared.application(
            app,
            open: url,
            sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,
            annotation: options[UIApplication.OpenURLOptionsKey.annotation]
        )
    }  
}

iOS 13 moved opening URL functionality to the SceneDelegate. If you are using iOS 13, add the following method to your SceneDelegate so that operations like logging in or sharing function as intended:
// SceneDelegate.swift
import FacebookCore
  ...
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    guard let url = URLContexts.first?.url else {
        return
    }

    ApplicationDelegate.shared.application(
        UIApplication.shared,
        open: url,
        sourceApplication: nil,
        annotation: [UIApplication.OpenURLOptionsKey.annotation]
    )
}

6. Add Facebook Login to Your Code

Use the Facebook Login button in your iOS app.

6a. Add Facebook Login to Your Code

To add a Facebook-branded Login button to your app, add the following code snippet to a view controller.
// Add this to the header of your file, e.g. in ViewController.swift 
import FacebookLogin

// Add this to the body
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
	
        let loginButton = FBLoginButton()
        loginButton.center = view.center
        view.addSubview(loginButton)
    }
}
At this point you should be able to run your app and log in using the Facebook Login button.

6b. Check Current Login Status

Your app can only have one person logged in at a time. We represent each person logged into your app with AccessToken.current.
The LoginManager sets this token for you and when it sets AccessToken.current it also automatically writes it to the keychain store.
The AccessToken contains userID which you can use to identify the user.
You should update your view controller to check for an existing token at load. This avoids unnecessary showing the login flow again if someone already granted permissions to your app:
override func viewDidLoad() {
    super.viewDidLoad()

    if let token = AccessToken.current,
        !token.isExpired {
        // User is logged in, do work such as go to next view controller.
    }
}
    

6c. Ask for Permissions

When using Facebook Login, your app can ask for permissions on a subset of a person's data.
Read Permissions for Facebook Login Button
To request additional read permissions, set the permissions property on the FBLoginButton object.
// Extend the code sample from 6a. Add Facebook Login to Your Code
// Add to your viewDidLoad method:
loginButton.permissions = ["public_profile", "email"]
  
The user will be prompted to grant your app with the requested permissions. Note that some permissions will require a Login Review. See Managing Permissions for more information on permissions.

7. Next Steps

Congrats, you've added Facebook Login to your iOS app! Be sure to check out our other documentation pages for more advanced guides.
Implement a data deletion callback to respond the people's request to delete their data from Facebook.
Add events to your app to view analytics, measure ad performance and build audiences for ad targeting.
Check out our advanced setup for Facebook Login for iOS apps.
Manage what data your app has access to through Facebook Login.
Check out how to responds to errors returned by the Facebook SDK.
Test and verify that your Facebook Login flow works.
Depending on the Facebook data you request from people using Facebook Login, you may need to submit your app for review prior to launch.