Implement the SDK

This guide shows you how to implement the Facebook SDK for iOS Swift into you mobile app. The SDK is distributed via CocoaPods and Carthage.

Use CocoaPods

Do the following steps to implement the SDK into your project using CocoaPods.

  1. In a Terminal window, go to the folder with the project.
  2. Run the following command to install CocoaPods:
    gem install cocoapods
  3. Run the following command to create a Podfile:
    pod init
  4. In a text editor, open the Podfile and add the following lines depending on which SDK you want to implement:
    To Use This SDKAdd This to the Podfile

    FacebookCore

    pod 'FacebookCore'

    FacebookLogin

    pod 'FacebookLogin'

    FacebookShare

    pod 'FacebookShare'
  5. Run the following command to install the SDKs:
    pod install

Use Carthage

Do the following steps to implement the SDK into your project using Carthage.

  1. In a Terminal window, go to the folder with the project.
  2. Run the following command to create and empty Cartfile:
    touch Cartfile
  3. In a text editor, open the Cartfile and add the following lines
    github "facebook/facebook-swift-sdk"
  4. In a Terminal window, run the following command to clone the Git repositories and then build the repositories into a framework.
    carthage bootstrap
  5. In Xcode, add the following frameworks depending on which SDK you want to implement:
    To Use This SDKAdd This Framework

    FacebookCore

    FacebookCore.framework

    FacebookLogin

    FacebookCore.framework
    FacebookLogin.framework

    FacebookShare

    FacebookCore.framework
    FacebookShare.framework

Initialize the SDK

To initialize the SDK, add the following code to your AppDelegate class of your project. The code initializes the SDK when your app launches, and lets the SDK handle results from the native Facebook app when you perform a Login or Share action.

import FacebookCore

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
  SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
  return true
}

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
  return SDKApplicationDelegate.shared.application(app, open: url, options: options)
}