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.

After you integrate Facebook Login, certain App Events are automatically logged and collected for Events Manager, unless you disable Automatic App Event Logging. In particular, when launching an app in Korea, please note that Automatic App Event Logging can be disabled. For details about what information is collected and how to disable automatic app event logging, see Automatic App Event Logging.

  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 the following:
    <key>LSApplicationQueriesSchemes</key>
    <array>
      <string>fbapi</string>
      <string>fb-messenger-share-api</string>
    </array>

You may directly set the automatic collection of App Events to “true” or “false” by adding FacebookAutoLogAppEventsEnabled as a key in Info.plist.

Your project will need to include the Keychain Sharing capability in order for login to work in Mac Catalyst applications.
  1. Select the + Capability button in the Signing & Capabilities tab when configuring your app target.
  2. Find and select the Keychain Sharing capability.
  3. Ensure that the Keychain Sharing capability is listed for the target.

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 to handle logins and sharing from the native Facebook app when you perform a Login or Share action. Otherwise, the user must be logged into Facebook to use the in-app browser to login.
    
// 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. Facebook Login requires advanced public_profile permission, to be used by external users.
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.
For building your own login flow, see Manually Build a Login Flow.