The Facebook SDK for iOS is the easiest way to integrate your iOS app with Facebook. The Facebook SDK enables:
This topic will help you get started using the Facebook SDK for iOS using SPM to import the Facebook components. If you use CocoaPods, see Get Started (CocoaPods).
This procedure assumes you are using the latest version of iOS and Xcode. Learn more at the Apple Developer.
Note: To follow these instructions, create a project with UIKit App Delegate selected for Life Cycle. The following image shows an example:
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 to enable engagement and promote social features |
|
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 |
|
To register and configure your app with Facebook, you add your Bundle ID and Enable Single Sign-On for your app.
Single Sign On Will launch from iOS Notifications |
Info.plist
file with an XML snippet that contains data about your app. Info.plist
, and choose Open As ▸ Source Code. <dict>...</dict>
). <array><string>
in the key [CFBundleURLSchemes]
, replace [APP_ID] with your App ID.<string>
in the key FacebookAppID
, replace [APP_ID] with your App ID.<string>
in the key FacebookDisplayName
, replace [APP_NAME] with the name of your app.Info.plist
also needs to include: <dict>...</dict>
). <key>LSApplicationQueriesSchemes</key> <array> <string>fbapi</string> <string>fbapi20130214</string> <string>fbapi20130410</string> <string>fbapi20130702</string> <string>fbapi20131010</string> <string>fbapi20131219</string> <string>fbapi20140410</string> <string>fbapi20140116</string> <string>fbapi20150313</string> <string>fbapi20150629</string> <string>fbapi20160328</string> <string>fbauth</string> <string>fb-messenger-share-api</string> <string>fbauth2</string> <string>fbshareextension</string> </array>
AppDelegate
method with the following code. This code initializes the SDK when your app launches, and allows the SDK handle results from the native Facebook app when you perform a Login or Share action. // Swift // // 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] ) } }
// Objective-C // // AppDelegate.m @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions]; return YES; } - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(nonnull NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options { [[FBSDKApplicationDelegate sharedInstance] application:application openURL:url options:options]; return YES; }
SceneDelegate
. If you are using iOS 13, add the following method to your SceneDelegate
so that operations like logging in or sharing function as intended: // Swift // // 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] ) }
// Objective-C // // SceneDelegate.m #import <FBSDKCoreKit/FBSDKCoreKit.h> @import FacebookCore; @interface SceneDelegate () @end @implementation SceneDelegate - (void)scene:(UIScene *)scene openURLContexts:(NSSet<UIOpenURLContext *> *)URLContexts { UIOpenURLContext *context = URLContexts.allObjects.firstObject; [FBSDKApplicationDelegate.sharedInstance application:UIApplication.sharedApplication openURL:context.URL sourceApplication:context.options.sourceApplication annotation:context.options.annotation]; }
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