When people log into your app with Facebook, they can grant permissions to your app so you can retrieve information or perform actions on Facebook on their behalf.
The following steps are for adding Facebook Login to your iOS Objective-C project. For adding Facebook Login to a Swift project, see Facebook Login in Swift - Getting Started.
If you link to the SDKs with CocoaPods, you must update your pods for the SDKs your app uses and recompile your app. You can also download the latest version of the Facebook iOS SDK, integrate it into your app, and recompile.
Set up your development environment before using Facebook Login for iOS.
*.xcworkspace
in your project folder in a terminal window.Podfile
in your project's root directory. info.plist
) with an XML snippet that contains data about your app. info.plist
, and choose Open As Source Code. <dict>...</dict>
). info.plist
also needs to include: -ObjC
to Other Linker Flags for all build targets.AppDelegate
class. This initializes the SDK when your app launches, and lets the SDK handle results from the native Facebook app when you perform a Login or Share action. // AppDelegate.m #import <FBSDKCoreKit/FBSDKCoreKit.h> - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions]; // Add any custom logic here. return YES; } - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options { BOOL handled = [[FBSDKApplicationDelegate sharedInstance] application:application openURL:url sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey] annotation:options[UIApplicationOpenURLOptionsAnnotationKey] ]; // Add any custom logic here. return handled; }
application:openURL:options:
is only available in iOS 9 and above. If you are building with an older version of the iOS SDK, you can use: - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { BOOL handled = [[FBSDKApplicationDelegate sharedInstance] application:application openURL:url sourceApplication:sourceApplication annotation:annotation ]; // Add any custom logic here. return handled; }
Use the Facebook Login button in your iOS app.
// Add this to the header of your file, e.g. in ViewController.m // after #import "ViewController.h" #import <FBSDKCoreKit/FBSDKCoreKit.h> #import <FBSDKLoginKit/FBSDKLoginKit.h> // Add this to the body @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init]; // Optional: Place the button in the center of your view. loginButton.center = self.view.center; [self.view addSubview:loginButton]; } @end
[FBSDKAccessToken currentAccessToken]
. FBSDKLoginManager
sets this token for you and when it sets currentAccessToken
it also automatically writes it to a keychain cache. FBSDKAccessToken
contains userID
which you can use to identify the user. readPermissions
property on the FBSDKLoginButton
object.