Este documento foi atualizado.
A tradução para Português (Brasil) não foi concluída ainda.
Atualização em inglês: 9 de abr
Atualização em Português (Brasil): 31 de jul de 2025

Getting Started with the Facebook SDK for iOS

This guide shows you how to integrate your iOS app with Facebook using the Facebook SDK for iOS.

The Facebook SDK enables:

Before You Start

You will need:

Step 1: Set Up Your Development Environment

  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, select Up to Next Major and the default option.
  4. Complete the prompts to select the libraries you want to use in your project.
  5. If You Want ToAdd This Package to your project

    Allow your app to use the Facebook services

    FBSDKCoreKit

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

    FBSDKLoginKit

    Allow your app to share content on Facebook

    FBSDKShareKit

    Allow users to log into your app to enable engagement and promote social features

    FBSDKGamingServicesKit

Step 2: Configure Your Project

Configure o arquivo Info.plist com um trecho em XML que contenha dados sobre o 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. Clique com o botão direito do mouse em Info.plist e selecione Abrir como ▸ Código-fonte.
  2. Copie e cole o seguinte trecho em XML no corpo do arquivo (<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. Em <array><string>, na chave [CFBundleURLSchemes], substitua APP-ID pelo ID do app.
  4. Em <string>, na chave FacebookAppID, substitua APP-ID pelo ID do app.
  5. Em <string>, na chave FacebookClientToken, substitua CLIENT-TOKEN pelo valor encontrado em Configurações > Avançado > Token de cliente no Painel de Apps.
  6. Em <string>, na chave FacebookDisplayName, substitua APP-NAME pelo nome do app.
  7. Para usar um diálogo do Facebook (por exemplo, o diálogo Entrar, o diálogo de compartilhamento, Convites para app do Facebook e assim por diante) que possa executar uma mudança para apps do Facebook, o Info.plist do app precisará incluir isto também:
    <key>LSApplicationQueriesSchemes</key><array><string>fbapi</string><string>fb-messenger-share-api</string></array>

É possível definir o processo de coleta automática de eventos do app como "verdadeiro" ou "falso" adicionando FacebookAutoLogAppEventsEnabled como uma chave em Info.plist.

É necessário que o projeto tenha o recurso Keychain Sharing para que o login funcione em apps do Mac Catalyst.

  1. Clique no botão + Capability na aba Signing & Capabilities ao configurar o alvo do app.
  2. Encontre e selecione o recurso Keychain Sharing.
  3. Verifique se o recurso Keychain Sharing está listado para o direcionamento.

Step 3: Connect the App Delegate

Substitua o código no método AppDelegate.swift pelo código a seguir. Esse código inicializa o SDK quando seu app é iniciado e permite que o SDK gerencie logins e compartilhamentos do app nativo do Facebook quando você executa uma ação Entrar ou Compartilhar. Caso contrário, o usuário precisará estar conectado ao Facebook para fazer login pelo navegador no app.

  
// 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]
        )
    }  
}

O iOS 13 moveu a funcionalidade da URL de abertura para SceneDelegate. Ao usar essa versão do iOS, adicione o seguinte método a SceneDelegate para que as operações de login e compartilhamento funcionem conforme o esperado:

 
// 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]
    )
}

Step 4: 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.

Step 5: See the Results in Events Manager


The Events Manager displays the events you send to Facebook. If this is the first time you launched your app with this code, you may have to wait at least 20 minutes before your events appear.

Note: Events may take up to 20 minutes to appear in the dashboard.

Next Steps

To learn how to implement App Events and other Facebook products to your app, click one of the buttons below.

Sharing in iOSAdd Facebook LoginAdd App EventsUse Graph API
Advanced Configuration