Facebook Login Using Native iOS API


If you want to add Facebook login support to your app, you have two main choices:

  • Use the native account management support built into iOS.
  • Use the API available from Facebook.

In this article I will explain how to use the first option. It has a few advantages:

  • User has to login to Facebook only once from the iOS Settings app.
  • There is no third party library to download and add to your app.

Register Your App

Go to http://facebook.com/developer and register your iOS app. You will need to supply the bundle ID of your app. You can get that from Xcode by selecting your target.


After you register the app, you will receive an application ID which is a large integer number.


We will need to use this app ID later.

Create the Facebook Account

Launch the iOS Settings app and click Facebook.


Log in to Facebook from there to add the account to your device.

Login from Your App

In your view controller’s header file, add two properties.

@import Accounts;

@interface ViewController : UIViewController

@property (nonatomic, strong) ACAccountStore *accountStore;
@property (nonatomic, strong) ACAccount *facebookAccount;


In the view controller class, add this method.

-(void) facebookLogin
    NSLog(@"Trying to access FB account.");

    self.accountStore = [[ACAccountStore alloc]init];
    ACAccountType *FBaccountType= [self.accountStore 

    NSString *appID = @"1540000000000031"; //The app ID issued by Facebook
    NSDictionary *dictFB = [NSDictionary dictionaryWithObjectsAndKeys:
                            appID, ACFacebookAppIdKey,
                            @[@"email"], ACFacebookPermissionsKey,

    [self.accountStore requestAccessToAccountsWithType:FBaccountType options:dictFB completion:
     ^(BOOL granted, NSError *e) {
         if (granted)
             NSArray *accounts = [self.accountStore accountsWithAccountType:FBaccountType];
             //it will always be the last object with single sign on
             self.facebookAccount = [accounts lastObject];

             ACAccountCredential *facebookCredential = [self.facebookAccount credential];
             NSString *accessToken = [facebookCredential oauthToken];
             NSLog(@"Successfully logged in. Access Token: %@", accessToken);
        } else {
             NSLog(@"error getting permission: %@",e);

Note that the login happens asynchronously. You must save the ACAccountStore object as a property. Otherwise it will go out of scope and get destroyed.

Finally, call the method from viewDidLoad.

- (void)viewDidLoad {
    [super viewDidLoad];
    //Login to Facebook
    [self facebookLogin];

2 thoughts on “Facebook Login Using Native iOS API

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s