[go: up one dir, main page]

Dieses Dokument wurde aktualisiert.
Die Übersetzung ins Deutsche ist noch nicht fertig.
Englisch aktualisiert: 26.03.2023

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.

Beginning with SDK v13.0 a Client Token is required for all calls to the Graph API.

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

    FacebookCore

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

    FacebookLogin

    Allow your app to share content on Facebook

    FacebookShare

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

    FacebookGamingServices

Step 2: Configure Your Project

Konfiguriere die Datei Info.plist mit einem XML-Ausschnitt, der Daten zu deiner App enthält.

Nachdem du Facebook Login integriert hast, werden App-Events automatisch für den Events Manager protokolliert und erfasst, es sei denn, du deaktivierst die automatische Protokollierung von App-Events. Detaillierte Informationen dazu, welche Daten erfasst werden und wie du die automatische Protokollierung von App-Events deaktivierst, findest du im Abschnitt Automatische Protokollierung von App-Events.

  1. Klicke mit der rechten Maustaste auf Info.plist und wähle „Öffnen als ▸ Quellcode“ aus.
  2. Kopiere den folgenden XML-Ausschnitt und füge ihn in den Textkörper deiner Datei ein (<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. Ersetze in <array><string> im Key [CFBundleURLSchemes]APP-ID durch deine App-ID.
  4. Ersetze in <string> im Key FacebookAppIDAPP-ID durch deine App-ID.
  5. Ersetze in <string> im Key FacebookClientTokenCLIENT-TOKEN durch den Wert, den du in deinem App-Dashboard unter Einstellungen > Erweitert > Client Token findest.
  6. Ersetze in <string> im Key FacebookDisplayNameAPP-NAME durch den Namen deiner App.
  7. Wenn du Facebook-Dialoge (wie z. B. „Login“, „Teilen“ oder „App-Einladungen“) verwendest, die einen App-Wechsel zu Facebook-Apps durchführen können, muss die Info.plist-Datei deiner App ebenfalls Folgendes aufweisen:
    <key>LSApplicationQueriesSchemes</key>
    <array>
      <string>fbapi</string>
      <string>fb-messenger-share-api</string>
    </array>

Du kannst die automatische Erfassung von App-Events direkt auf „true“ oder „false“ setzen, indem du FacebookAutoLogAppEventsEnabled als Schlüssel in Info.plist hinzufügst.

Du musst in deinem Projekt die Funktion „Schlüsselbund teilen“ aufnehmen, damit die Anmeldung in Mac Catalyst-Anwendungen funktioniert.
  1. Wähle den Button + Funktion im Tab Anmeldung und Funktionen, wenn du dein App-Ziel konfigurierst.
  2. Wähle die Funktion Schlüsselbund teilen aus.
  3. Stelle sicher, dass die Funktion Schlüsselbund teilen für das Ziel aufgeführt ist.

Step 3: Connect the App Delegate

Ersetze den Code in der AppDelegate.swift Methode durch folgenden Code. Dieser Code initialisiert das SDK beim Start der App und ermöglicht dem SDK, die Anmeldungen und Teilen-Vorgänge aus der nativen Facebook-App zu verarbeiten, wenn du einen Anmelde- oder Teilen-Vorgang durchführst. Ansonsten muss der*die Benutzer*in bei Facebook angemeldet sein, um den Browser in der App für die Anmeldung zu verwenden.
    
// AppDelegate.swift
import UIKit
import FacebookCore

@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 hat das Öffnen der URL-Funktionalität verschoben auf SceneDelegate. Wenn du iOS 13, füge die folgende Methode zu deinem SceneDelegate damit Vorgänge wie das Anmelden oder die Freigabe ordnungsgemäß funktionieren:
// SceneDelegate.swift
import FacebookCore
  ...
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

Wähle in Xcode einen iOS-Simulator aus und klicken Sie auf Ausführen. Xcode erstellt dein Projekt und startet dann die aktuelle Version deiner in Simulator ausgeführten App.

Step 5: See the Results in Events Manager


Der Events Manager zeigt die Events an, die du an Facebook sendest. Wenn du die App zum ersten Mal mit diesem Code gestartet hast, musst du möglicherweise mindestens 20 Minuten warten, bevor die Events angezeigt werden.
Hinweis: Es kann bis zu 20 Minuten dauern, bis Events im Dashboard erscheinen.

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