Get Started: Facebook SDK (Cocoa Pods)

The Facebook SDK for iOS is the easiest way to integrate your iOS app with Facebook. The Facebook SDK enables:

This topic will help you get started using the Facebook SDK for iOS using CocoaPods. If you typically use Swift Package Manager (SPM), see Get Started (SPM).

Before You Start

This procedure assumes you are using the latest version of iOS and xCode. Learn more at the Apple Developer.

You will need:

A Facebook Developer Account

An App ID for your app

Step 1: Set Up Your Development Environment

In this step you will install CocoaPods and create a pod file.

  1. Open a terminal window and run the following command:

    sudo gem install cocoapods

  2. Run the following command to create a pod file for your project:

    pod init

Step 2: Specify and Install the Facebook Components

In this step, you will specify the parts of the of the Facebook SDK your app will need to import. You can add one or more components depending on the app requirements.

Open the pod file in a text editor and add the line for the component you wish to install.

If You Want ToAdd This Line to Your Podfile

Allow your app to use the Facebook services

pod 'FacebookCore'

Allow users to log into your app and for your app to ask for permissions to access data

pod 'FacebookLogin'

Allow your app to share content on Facebook

pod 'FacebookShare'

  1. Save the pod file.

  2. Run the following command to install the components you specified in your pod file:

pod install

Learn more about the Facebook SDK APIs in the Reference Guide.

Step 4: Register and Configure Your App with Facebook

To register your app with Facebook, add Your Bundle Identifier and enable Single Sign-On for your App

The bundle identifier (Bundle ID) should appear in the box below. If the box is empty, find your bundle identifier in your Xcode Project's iOS Application Target and paste it into the box below.
Bundle ID
You can change your bundle identifier in the future via the iOS section on the settings page.
Enable Single Sign On
Enable single sign on for your app by setting Single Sign On to Yes below.
Single Sign On Will launch from iOS Notifications

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>fb[APP_ID]</string>
      </array>
      </dict>
    </array>
    <key>FacebookAppID</key>
    <string>[APP_ID]</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 FacebookDisplayName, replace [APP_NAME] with the name of your app.
  6. 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>

Step 5: Connect App Delegate

Upon initialization, the SDK will perform various network calls to fetch configuration information. If you would like to defer these operations you may want to wrap initialization in a method that can conditionally run code based on a server response. There are a number of ways to do this. The following code block is pseudocode to demonstrate what a potential solution might look like.
FlagFetcher.fetchFlags { result in
    if case let .success(flags) = result,
      flags.contains("use_facebook") {
        // Initialize the SDK
        ApplicationDelegate.shared.application(
            application,
            didFinishLaunchingWithOptions: launchOptions
        )			
    }
}      
    
Replace the code in the AppDelegate 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.

Swift

    
// Swift
//
// AppDelegate.swift
import UIKit
import FBSDKCoreKit

@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]
        )

    }  

}
    

Objective C

// Objective-C
//
// AppDelegate.m  
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  
  [[FBSDKApplicationDelegate sharedInstance] application:application
                           didFinishLaunchingWithOptions:launchOptions];
  return YES;
}


- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
            options:(nonnull NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options
{
  [[FBSDKApplicationDelegate sharedInstance] application:application
                                                 openURL:url
                                                 options:options];
  return YES;
}
    

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:

Swift

// Swift
//
// SceneDelegate.swift
import FBSDKCoreKit
  ...
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]
    )
}

    

Objective C

// Objective-C
//
// SceneDelegate.m
#import <FBSDKCoreKit/FBSDKCoreKit.h>

@import FacebookCore;

@interface SceneDelegate ()

@end

@implementation SceneDelegate

- (void)scene:(UIScene *)scene openURLContexts:(NSSet<UIOpenURLContext *> *)URLContexts
{
  UIOpenURLContext *context = URLContexts.allObjects.firstObject;
  [FBSDKApplicationDelegate.sharedInstance application:UIApplication.sharedApplication
                                               openURL:context.URL
                                     sourceApplication:context.options.sourceApplication
                                            annotation:context.options.annotation];
}
    

Step 6: Build and Then Run Your Project in the Simulator

In Xcode, select an iOS simulator and click Run. Xcode builds your project and then launches the most recent version of your app running in Simulator.