This guide shows you how to add App Events to your new or existing app by integrating the Facebook SDK then logging these events.
Changes have been made to the Facebook iOS SDK. We recommend upgrading to the new version of the Facebook iOS SDK. See the Device Consent section for more information about this change.
You will need:
Go to the App Dashboard, click My Apps, and create a new app if you don't already have one. Navigate to Settings > Basic to view the App Details Panel with your App ID, your App Secret, and other details about your app.
Scroll down to the bottom of the page and click Add Platform. Choose iOS, add your app details, and save your changes.
Set up your app for advertising by adding the following details:

To learn more about adding details to your app, such as an icon or category, visit the App Development docs.
To run ads and measure installs in the Ads Manager, associate at least one ad account and a business portfolio with your App.
The following procedure uses Swift Package Manager to set up your development environment in Xcode.
Configura il file Info.plist con uno snippet XML contenente i dati sulla tua 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 scegli Apri come ▸ Codice sorgente.
<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> nel codice [CFBundleURLSchemes], sostituisci APP-ID con l'ID della tua app.<string> nel codice FacebookAppID, sostituisci APP-ID con l'ID della tua app.<string> nel codice FacebookClientToken, sostituisci CLIENT-TOKEN con il valore riportato in Impostazioni > Avanzate > Token client nella Dashboard gestione app.<string> nel codice FacebookDisplayName, sostituisci APP-NAME con il nome della tua app.Info.plist della tua app deve includere anche quanto segue:
<key>LSApplicationQueriesSchemes</key><array><string>fbapi</string><string>fb-messenger-share-api</string></array>
Puoi impostare direttamente la raccolta automatica degli eventi nell'app su "true" o "false" aggiungendo FacebookAutoLogAppEventsEnabled come chiave in Info.plist.
Affinché l'accesso funzioni in applicazioni Mac Catalyst, il progetto dovrà includere la funzionalità Keychain Sharing.



Sostituisci il codice nel metodo AppDelegate.swift con il codice seguente. Il codice inizializza l'SDK all'avvio dell'app e consente all'SDK di gestire gli accessi e la condivisione dall'app nativa di Facebook quando esegui un'azione di accesso o condivisione. In caso contrario, l'utente deve aver effettuato l'accesso a Facebook per utilizzare il browser in-app per accedere.
// 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]
)
}
}
iOS 13 ha spostato la funzionalità dell'URL di apertura a SceneDelegate. Se usi iOS 13, aggiungi il seguente metodo a SceneDelegate in modo che operazioni come l'accesso o la condivisione funzionino come previsto:
// 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]
)
}
There are three ways events are tracked in your app:
Gli eventi raccolti e inviati a Facebook dalla tua app potrebbero richiedere la divulgazione di questi tipi di dati nel questionario App Store Connect. È tua responsabilità assicurarti che ciò sia adeguatamente indicato nell'informativa sulla privacy della tua app. Consulta l'articolo Dettagli sulla privacy dell'App Store di Apple per maggiori informazioni sui tipi di dati che dovrai divulgare.
When using the Facebook SDK, certain events in your app are automatically logged and collected for Facebook Events Manager unless you disable automatic event logging. These events are relevant for all use cases - targeting, measurement and optimization.
There are three key events collected as part of the Automatic App Event Logging: App Install, App Launch, and Purchase. When automatic logging is enabled, advertisers are able to disable these events, as well as other Facebook internal events such as login impression events. However, if you have disabled automatic logging, but still want to log specific events, such as install or purchase events, manually implement logging for these events in your app.
| Event | Details |
|---|---|
App Install | The first time a new user activates an app or the first time an app starts on a particular device. |
App Launch | When a person launches your app, the Facebook SDK is initialized and the event is logged. However, if a second app launch event occurs within 60 seconds of the first, the second app launch event is not logged. |
In-App Purchase | When a purchase processed by the Apple App Store or Google Play has been completed. If you use other payments platforms, you will need to add purchase event code manually. Note: If you’d like to use in-app purchases to measure Dynamic Ads conversions, please set the Product ID on the Apple App Store or the Google Play store to be equivalent to the Product ID used in the associated Dynamic Ad. |
Facebook SDK Crash Report (For Facebook Use Only.) | If your app crashed due to the Facebook SDK, a crash report is generated and sent to Facebook when your app is restarted. This report contains no user data and helps Facebook ensure the quality and stability of the SDK. To opt out of logging this event, disable automatically logged events. |
Apple provides four different In-app purchase types: consumable, non-consumable, auto-renewable subscription, and non-renewing subscription. If you implement In-App Purchases with StoreKit 1, we will automatically log each of these In-app purchase types. If you implement In-App Purchases with StoreKit 2, we will automatically log non-consumables, auto-renewable subscriptions, and non-renewing subscriptions. If you would like to also automatically log consumables, you will need to add the SKIncludeConsumableInAppPurchaseHistory key to your Info.plist:
<key>SKIncludeConsumableInAppPurchaseHistory</key>
<true/>
In StoreKit 1, we will automatically log an event when the user successfully purchases a product, restores a product, or attempts to purchase a product but the purchase fails. In Store Kit 2, we will automatically log an event when the user successfully purchases a product or restores a product. If you would like to also log when a purchase fails in Store Kit 2, we have provided a manual API you must call. You can call this API in your StoreKit 2 purchase flow in the following way:
do {
let result = try await product.purchase()
switch result {
case .success(let verificationResult):
// Handle success case
case .pending:
// Handle pending case
default:
AppEvents.shared.logFailedStoreKit2Purchase(product.id)
}
} catch {
AppEvents.shared.logFailedStoreKit2Purchase(product.id)
}
Starting with iOS 14.5, you will need to set isAdvertiserTrackingEnabled and log each time you give a device permission to share data with Facebook.
If a device provides consent, set Settings.shared.isAdvertiserTrackingEnabled = true.
If a device does not allow tracking, set Settings.shared.isAdvertiserTrackingEnabled = false.
To disable automatic event logging, open the application's Info.plist as code in Xcode and add the following XML to the property dictionary:
<key>FacebookAutoLogAppEventsEnabled</key> <false/>
In some cases, you want to delay the collection of automatically logged events, such as to obtain User consent or fulfill legal obligations, instead of disable it. In this case, set Settings.shared.isAutoLogAppEventsEnabled = true to re-enable auto-logging after the end-user provides consent.
To suspend collection again for any reasons, set Settings.shared.isAutoLogAppEventsEnabled = false.
You can also disable automatic In-App Purchase event logging using the app dashboard. Go to the iOS card under Basic>Settings and toggle the switch to No.
To disable collection of advertiser-id, open the application's .plist as code in Xcode and add the following XML to the property dictionary:
<key>FacebookAdvertiserIDCollectionEnabled</key> <false/>
In some cases, you want to delay the collection of advertiser_id, such as to obtain User consent or fulfill legal obligations, instead of disabling it. In this case, set Settings.shared.isAdvertiserIDCollectionEnabled = true after the end-user provides consent.
To suspend collection for any reason, set Settings.shared.isAdvertiserIDCollectionEnabled = false.
To log a custom event, just pass the name of the event as an AppEvents.Name:
AppEvents.shared.logEvent(AppEvents.Name("battledAnOrc"))Meta has created a set of useful event parameters for inclusion with standard events or with your own custom events. You can also provide your own parameters.
If you’d like to use app events to measure Dynamic Ads conversions, please set the fb_content_id parameter to be the value of the Product ID used in the associated Dynamic Ad.
These pre-defined parameters are intended to provide guidance on common logging patterns, and may have a more readable form in reporting and other UIs. Log the set of parameters you're interested in seeing broken down. The recommended description for these are guidance only - you can use these parameters for whatever makes sense for your app.
The parameters are passed via a dictionary where the key holds the parameter name as an AppEvents.ParameterName, and the value must be either a String or a number (Int, Double, etc.).
The App Ads Helper allows you to test the app events in your app to ensure that your app is sending events to Facebook.
If you plan to optimize/track your events in SKAdNetwork campaigns, you also need to properly configure event priority (also known as conversion value) in order for Facebook to correctly receive the conversions. More details can be found here.
We have created some examples for different app types to make it easier for you to see how you can use app events. Each of the example apps provides a screen by screen breakdown of the different events and parameters that can be collected. At the end of each section, there is a table listing the recommended events and parameters for each app. And, if necessary, you can create your own events and parameters.