Facebook Login in Swift - Getting Started

The Facebook SDK in Swift 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 that you can retrieve information or perform actions on the Facebook platform on their behalf.

Login Button

To add a Facebook login button to your app add the following code snippet to a view controller.

import FacebookLogin

func viewDidLoad() {
    let loginButton = LoginButton(readPermissions: [ .PublicProfile ])
    loginButton.center = view.center

    view.addSubview(loginButton)
}

Screenshots: Login & Logout Flow

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

Login Button

Logout Button

Login Dialog

Logout Dialog

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 AccessToken.current. The LoginManager sets this token for you and when it sets current it also automatically writes it to a keychain cache. The AccessToken contains userId which is used to identify the user.

You should update your view controller to check for an existing token at load. This eliminates an unnecessary app switch to Facebook if someone has already granted permissions to your app. Example:

func viewDidLoad() {
  if let accessToken = AccessToken.current {
   // User is logged in, use 'accessToken' here.
  }
}

Ask for Permissions

When you add Facebook Login, your app can ask someone for permissions to access a subset of that person's data or perform actions on their behalf.

Read Permissions for Facebook Login Button

For LoginButton use the readPermissions constructor to request additional read permissions.

In your view header file add:

// Extend the code sample "1. Add Facebook Login Button Code"
// In your viewDidLoad method:
loginButton = LoginButton(readPermissions: [ .PublicProfile, .Email, .UserFriends ])

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 LoginButton that confirms to the LoginButtonDelegate.

Custom Login Button

Instead of using the Facebook login button, you may want to design a custom layout and behavior. In the following code example we invoke the login dialog using the LoginManager class and a UIButton button object. You can use any other custom user interface or event handling to invoke the login dialog.

import FacebookCore
import FacebookLogin

func viewDidLoad() {    
  // Add a custom login button to your app
  let myLoginButton = UIButton(type: .Custom)]
  myLoginButton.backgroundColor = UIColor.darkGrayColor()
  myLoginButton.frame = CGRect(0, 0, 180, 40);
  myLoginButton.center = view.center;
  myLoginTitle.setTitle("My Login Button" forState: .Normal)

  // Handle clicks on the button
  myLoginButton.addTarget(self, action: @selector(self.loginButtonClicked) forControlEvents: .TouchUpInside) 

  // Add the button to the view
  view.addSubview(myLoginButton)
}

// Once the button is clicked, show the login dialog
@objc func loginButtonClicked() {
  let loginManager = LoginManager()
  loginManager.logIn([ .PublicProfile ], viewController: self) { loginResult in
    switch loginResult {
    case .Failed(let error): 
      print(error)
    case .Cancelled:
      print("User cancelled login.")
    case .Success(let grantedPermissions, let declinedPermissions, let accessToken):
      print("Logged in!")
  }
}

Login Review

If your app asks for more than public_profile, email, and user_friends, Facebook must review it before you release it. Learn more about the review process and app requirements.

Login Review Guide