Facebook Login for iOS - Quickstart

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.

1. Login

Please log in to Facebook to create apps or register as a developer.

2. Set up Your Development Environment

Set up your development environment before using Facebook Login for iOS.

2a. Download the Facebook SDK for iOS

Download the SDK and unzip the archive to ~/Documents/FacebookSDK

2b. Add Login Kit to your Xcode Project

  1. Open ~/Documents/FacebookSDK
  2. Drag the FBSDKLoginKit to Frameworks in Project Navigator.

3. Register and Configure Your App with Facebook

Register and configure your app so you can use Facebook Login.

3a. Add your Bundle Identifier

You need to login to complete this step.

3b. Enable Single Sign-On for Your App

You need to login to complete this step.

4. Configure Your info.plist

Find the .plist file in the Supporting Files folder in your Xcode Project.
  1. Right-click your .plist file and choose "Open As Source Code".
  2. Copy and paste the following XML snippet into the body of your file ( <dict>...</dict>).
    <key>CFBundleURLTypes</key>
    <array>
      <dict>
      <key>CFBundleURLSchemes</key>
      <array>
        <string>fb[APP_ID]</string>
      </array>
      </dict>
    </array>
    <key>FacebookAppID</key>
    <string>[APP_ID]</string>
    <key>FacebookDisplayName</key>
    <string>[APP_NAME]</string>
  3. If you use any of the Facebook dialogs (e.g., Login, Share, App Invites, etc.) that can perform an app switch to Facebook apps, your application's .plist also need to handle this.
    <key>LSApplicationQueriesSchemes</key>
    <array>
      <string>fbapi</string>
      <string>fb-messenger-api</string>
      <string>fbauth2</string>
      <string>fbshareextension</string>
    </array>

5. Connect Your App Delegate

To post-process the results from actions that require you to switch to the native Facebook app or Safari, such as Facebook Login or Facebook Dialogs, you need to connect your AppDelegate class to the FBSDKApplicationDelegate object. To accomplish this, add the following code to your AppDelegate.m file.
//  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 
    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;
} 
    

6. Add Facebook Login to Your Code

Use the Facebook Login button in your iOS app.

6a. Add Facebook Login to Your Code

To add a Facebook-branded login button to your app, add the following code snippet to a view controller.
// 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

6b. Check Current Login Status

Your app can only have one person logged in at a time. We represent each person logged into your app with the [FBSDKAccessToken currentAccessToken].
The FBSDKLoginManager sets this token for you and when it sets currentAccessToken it also automatically writes it to a keychain cache.
The FBSDKAccessToken contains userID which you can use to identify the user. If you need additional profile context, see Profiles.
You should update your view controller to check for an existing token at load. This eliminates an unnecessary app switch to Facebook if someone already granted permissions to your app:
- (void)viewDidLoad
{
  [super viewDidLoad];
  if ([FBSDKAccessToken currentAccessToken]) {
   // User is logged in, do work such as go to next view controller. 
  }
}

6c. Ask for Permissions

When you add Facebook Login, your app can ask someone for permissions on a subset of that person's data.
Read Permissions for Facebook Login Button
For the Facebook-branded login button (FBSDKLoginButton) use the readPermissions property to request additional read permissions.
In your view header file add:
//  ViewController.h
#import <FBSDKLoginKit/FBSDKLoginKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet FBSDKLoginButton *loginButton;
@end
In your view file add:
      
// Extend the code sample provided in "7. Add Facebook Login Button Code"
// In your viewDidLoad method:
loginButton.readPermissions = 
  @[@"public_profile", @"email", @"user_friends"];
Your app will now ask for the person's email address and friend list.

7. Next Steps

Congrats, you've added Facebook Login to your iOS app! Be sure to check out our other documentation pages for more advanced guides.
Add events to your app to view analytics, measure ad performance and build audiences for ad targeting.
Check out our advanced setup for Facebook Login for iOS apps.
Manage what data your app has access to through Facebook Login.
Check out how to responds to errors returned by the Facebook SDK.
Test and verify that your Facebook Login flow works.
Submit your app for review to ensure the best possible Facebook experience for your app's audience.