Account Kit for iOS - Quickstart

See Next Steps for optional customizations you can perform.

For an example project that illustrates how to integrate Account Kit, see Account Kit Samples for iOS on GitHub.

For common issues encountered during integration, see the FAQ section.

1. Login

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

2. Choose Your App Settings

Choose whether to allow email and SMS login, and choose security settings for your app. For more information on choosing your access token setting, see Access Tokens, and for information on choosing your app secret setting, see Using the Graph API.

3. Set up Your Developer Environment

Using Cocoapods

To always link to the most recent version, link to the Account Kit SDK with a CocoaPod rather than downloading the SDK itself.
  1. Navigate to your project folder in a terminal window.
  2. Make sure you have the CocoaPods gem installed on your machine before installing the Account Kit pod.
        $ sudo gem install cocoapods
        $ pod init
        
    This will create a file named Podfile in your project's root directory.
  3. Add the following to your Podfile:
        pod 'AccountKit'
        
  4. Run the following command in your project root directory from a terminal window:
        $ pod install
        
  5. Add both your Facebook App ID and Account Kit Client Token to your Info.plist file as strings. Make sure you have enabled Account Kit in the App Dashboard. You'll find the Account Kit client token in the Account Kit section of the App Dashboard. The application name will be used in the UI of the login screen.
    <plist version="1.0">
    <dict>
      ...
      <key>FacebookAppID</key>
      <string>{your-app-id}</string>
      <key>AccountKitClientToken</key>
      <string>{your-account-kit-client-token}</string>
      <key>CFBundleURLTypes</key>
      <array>
        <dict>
          <key>CFBundleURLSchemes</key>
          <array>
            <string>ak{your-app-id}</string>
          </array>
        </dict>
      </array>
      ...
    </dict>
    </plist>
    
    Remember to fill in your app ID for both the FacebookAppID and CFBundleURLSchemes keys.

Using the Facebook SDK

Download the SDK

Download the latest Facebook SDK for iOS and use it to integrate your app with Account Kit.
Overview: The iOS SDK provides View Controllers which you can present to initiate the SMS or Email login flows.

Configure the SDK

Add Facebook app ID and Account Kit client token to Info.plist
Add both your Facebook App ID and Account Kit Client Token to your Info.plist file as strings. Make sure you have enabled Account Kit in the App Dashboard. You'll find the Account Kit client token in the Account Kit section of the App Dashboard. The application name will be used in the UI on the login screen.

<plist version="1.0">
<dict>
  <key>FacebookAppID</key>
  <string>{your-app-id}</string>
  <key>AccountKitClientToken</key>
  <string>{your-account-kit-client-token}</string>
  <key>CFBundleURLTypes</key>
  <array>
    <dict>
      <key>CFBundleURLSchemes</key>
      <array>
        <string>ak{your-app-id}</string>
      </array>
    </dict>
  </array>
  ...
</dict>
</plist>

  
If you wish to disable App Events for your Account Kit application, add the following entry to your Info.plist file inside the <dict> tag:

<key>AccountKitFacebookAppEventsEnabled</key>
<integer>0</integer>

  
By default, this flag is set to 1. See App Events and Analytics for more information.
Link Account Kit Framework
From the SDK, drag and drop AccountKit.framework and AccountKitStrings.bundle to link them to your iOS project.

Initialize the SDK

In your initial view controllers, import the Account Kit headers and declare an accountKit variable to manage all Account Kit interactions. Declare the view controller to be an implementer of AKFViewControllerDelegate.

#import <AccountKit/AccountKit.h>

@interface LoginViewController () <AKFViewControllerDelegate>
@end

@implementation LoginViewController
{
  AKFAccountKit *_accountKit;
  UIViewController<AKFViewController> *_pendingLoginViewController;
  NSString *_authorizationCode;
}

   
  
When your initial view controller is loaded, check and initialize your accountKit variable. Be sure the response type you use to initialize Account Kit matches the one selected in your app's dashboard in the Facebook developer portal, either AKFResponseTypeAccessToken or AKFResponseTypeAuthorizationCode.

- (void)viewDidLoad
{
  [super viewDidLoad];

  // initialize Account Kit
  if (_accountKit == nil) {
    // may also specify AKFResponseTypeAccessToken
    _accountKit = [[AKFAccountKit alloc] initWithResponseType:AKFResponseTypeAuthorizationCode];
  }

  // view controller for resuming login
  _pendingLoginViewController = [_accountKit viewControllerForLoginResume];
}

  

4. Configure Login View Controller

You can configure your login view controller with the following parameters:
ParameterDescription
delegateAKFViewControllerDelegate: Pass in the delegate handling the login callback.
enableSendToFacebokBOOL: Set to YES to allow receiving Facebook notifications (if available) as a backup method of verification.
enableGetACallBOOL: Set to YES to allow receiving phone calls as backup verification method.
uiManagerAKFUIManager: Pass in either an instance of AKFSkinManager or a class implementing AKFAdvancedUIManager if you would like to use the Advanced UI. Otherwise you can pass in nil. For more information, see Customizing the UI.
The delegate for your loginViewController must implement the AKFViewControllerDelegate protocol. All of the protocol methods are optional, but you should at least handle successful login callbacks for the login flows (SMS or Email) that you use in your app. Set up your main view controller to receive login callbacks:
#import <AccountKit/AKFAccountKit.h>

@interface ViewController () <AKFViewControllerDelegate>
Prepare the Account Kit login view controller by setting a delegate as shown in the following code block.
- (void)_prepareLoginViewController:(UIViewController<AKFViewController> *)loginViewController 
{
  loginViewController.delegate = self;
  // Optionally, you may set up backup verification methods.
  viewController.enableSendToFacebook = YES;
  viewController.enableGetACall = YES;
}

UI Theming

(optional) You can create a classic skin UI with blue color using the following code:
  viewController.uiManager = [[AKFSkinManager alloc]
                              initWithSkinType:AKFSkinTypeClassic
                              primaryColor:[UIColor blueColor]]
You may apply a theme or use the Advanced UI Manager if you want to customize the user interface. For more information, see Customizing the UI.

5. Handle Different Login States

When your initial view controller appears, you should bypass the login view controller if the user is already logged in. It will also resume pending logins if any are present.
Your view controller needs to maintain a handle to an AKFAccountKit object at all times. You can declare this using the following code:
@interface ViewController () <AKFViewControllerDelegate>
{
    AKFAccountKit * _accountKit;
}
@end
To initialize Account Kit, we recommend doing this in the viewDidLoad event of your view controller. The following code initializes Account Kit to use the token access flow:
- (void)viewDidLoad {
    [super viewDidLoad];
    _accountKit = [[AKFAccountKit alloc]
                   initWithResponseType:AKFResponseTypeAccessToken];
}
If your app receives the user's access token directly (because the Enable Client Access Token Flow switch in your app's dashboard is ON), then you should check for a valid, existing token using [_accountKit currentUserToken].
- (void)viewWillAppear:(BOOL)animated
{
  [super viewWillAppear:animated];

  if ([_accountKit currentAccessToken]) {
    // if the user is already logged in, go to the main screen
    [self proceedToMainScreen];
  } else {
    // Show the login screen
    [self showLoginControls];
  }
}
If your app receives an authorization code that it will pass to the server (because the Enable Client Access Token Flow switch in your app's dashboard is OFF), it is up to you to have your server communicate the correct login status to your client application and return that.
For more information on access tokens and authorization codes, see Access Tokens and Authorization Codes.

6. Initiate a Login Flow for SMS

In your initial view controller, create a phone login handler to invoke when the login button is clicked by the user. There are two important parameters shown in the code below:
ParameterDescription
preFillPhoneNumbernullable NSString*
Pass this to pre-fill the phone number field in the phone login flow. Use nil to skip pre-filling.
inputStatenullable NSString*
This should be a random, non-guessable value. Any value passed via this parameter will be returned with the login response; compare the value in the response with this value to verify that the response you received was for the request you issued.
- (void)loginWithPhone:(id)sender
{
  NSString *preFillPhoneNumber = nil;
  NSString *inputState = [[NSUUID UUID] UUIDString];
  UIViewController<AKFViewController> *viewController =
    [_accountKit viewControllerForPhoneLoginWithPhoneNumber:preFillPhoneNumber
                                                      state:inputState];
  [self _prepareLoginViewController:viewController]; // see above
  [self presentViewController:viewController animated:YES completion:NULL];
}

7. Initiate a Login Flow for Email

In your initial view controller, create an email login handler to invoke when the login button is clicked by the user. There are two important parameters shown in the code below:
ParameterDescription
preFillEmailAddressnullable NSString*
Pass this to pre-fill the email address field in the email login flow. Use nil to skip pre-filling.
inputStatenullable NSString*
This should be a random, non-guessable value. Any value passed via this parameter will be returned with the login response; compare the value in the response with this value to verify that the response you received was for the request you issued.
- (void)loginWithEmail:(id)sender
{
  NSString *preFillEmailAddress = nil;
  NSString *inputState = [[NSUUID UUID] UUIDString];
  UIViewController<AKFViewController> *viewController =
    [_accountKit viewControllerForEmailLoginWithEmail:preFillEmailAddress
                                                state:inputState];
  [self _prepareLoginViewController:viewController]; // see above
  [self presentViewController:viewController animated:YES completion:NULL];
}

8. Handle Login Callback

How you handle a login depends on whether your app uses an access token or an authorization code. For more information, see Access Tokens and Authorization Codes.
To handle a successful login in Access Token mode:
// handle callbacks on successful login to show account
- (void)didCompleteLoginWithAccessToken:(id<AKFAccessToken>)accessToken
                         viewController:(UIViewController<AKFViewController> *)viewController
                                  state:(NSString *)state
{
  [self proceedToMainScreen];
}
To handle a successful login in Authorization Code mode:
// handle callback on successful login to show authorization code
- (void)didCompleteLoginWithAuthorizationCode:(NSString *)code
                               viewController:(UIViewController<AKFViewController> *)viewController
                                        state:(NSString *)state
{
  // Pass the code to your own server and have your server exchange it for a user access token.
  // You should wait until you receive a response from your server before proceeding to the main screen.
  [self sendAuthorizationCodeToServer:code];
  [self proceedToMainScreen];
}
You may also handle a failed or canceled login:
- (void)didFailWithError:(NSError *)error
          viewController:(UIViewController<AKFViewController> *)viewController 
{
  // ... implement appropriate error handling ...
  NSLog(@"%@ did fail with error: %@", viewController, error);
}
- (void)viewControllerDidCancel:(UIViewController<AKFViewController> *)viewController
{
  // ... handle user cancellation of the login process ...
}

9. Access Account Information

Once you have successfully logged in, you can access account information. For example, to display the account ID and the login credential used in access token mode:
[_accountKit requestAccount:^(id<AKFAccount> account, NSError *error) {
  // account ID
  self.accountIDLabel.text = account.accountID;
  if ([account.emailAddress length] > 0) {
    self.titleLabel.text = @"Email Address";
    self.valueLabel.text = account.emailAddress;
  } 
  else if ([account phoneNumber] != nil) {
    self.titleLabel.text = @"Phone Number";
    self.valueLabel.text = [[account phoneNumber] stringRepresentation];
  }
}];
To display the account ID and the login credential used in Authorization Code mode, call your server to use the Graph API with the exchanged access token stored on your server (see Access Token Validation) and have your server pass the account ID and credential back to your app.

10. Provide the Logout Flow

You can invoke the logOut method to log a user out of Account Kit.
[_accountKit logOut];