This guide shows you how to integrate your iOS app with Facebook using the Facebook SDK for iOS.
The Facebook SDK enables:
You will need:
| If You Want To | Add This Package to your project |
|---|---|
Allow your app to use the Facebook services |
|
Allow users to log into your app and for your app to ask for permissions to access data |
|
Allow your app to share content on Facebook |
|
Allow users to log into your app to enable engagement and promote social features |
|
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.
Info.plist e selecione Abrir como ▸ Código-fonte.
<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>
<array><string>, na chave [CFBundleURLSchemes], substitua APP-ID pelo ID do app.<string>, na chave FacebookAppID, substitua APP-ID pelo ID do app.<string>, na chave FacebookClientToken, substitua CLIENT-TOKEN pelo valor encontrado em Configurações > Avançado > Token de cliente no Painel de Apps.<string>, na chave FacebookDisplayName, substitua APP-NAME pelo nome do app.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.



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