Facebook Login for iOS - Getting Started

The Facebook SDK for iOS enables people to sign into your app with Facebook Login. 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. General iOS Setup

2. Add Login Kit to your Xcode Project

3. Add Facebook Login Button Code

4. Check Current Login Status

5. Ask for Permissions


Custom Login Button

Login Review

Next Steps

1. General iOS Setup

Before you add Facebook Login you need to complete the "Getting Started" guide for the Facebook SDK for iOS. Once completed, come back to this doc.

Facebook SDK for iOS - Getting Started

2. Add Login Kit to your Xcode Project

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

3. Add Facebook Login Button 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];

Screenshots: Login & Logout Flow

Compile and run your app. If your implementation was completed correctly you should be able to replay the following login and logout flow in your app:

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. 

4. 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;

In your view file add:

// Extend the code sample "1. 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.

Optional: To get notifications of login results or logout events assign a delegate to FBSDKLoginButton that confirms to FBSDKLoginButtonDelegate protocol.

Advanced Guide for Permissions

To learn how to ask for permissions using the login manager class (FBSDKLoginManager) or publishing permissions please read our Managing Permissions in iOS guide.

Managing Permissions in iOS

Facebook Login - Reference Documentation

Visit our Facebook SDK for iOS reference documentation to learn more about the classes used in this guide.

Class Description

FBSDKLoginManager (Login Manager)

Initiates the login flow.


FBSDKLoginButton (Login Button)

UIButton wraps around FBSDKLoginManager to provide easiest login option to implement.


FBSDKAccessToken (Access Token)

Represents the access token provided by a successful login. Most important, it provides a global +currentAccessToken to represent the currently logged in user.