Sign into Your Mobile App Using Facebook Logging

This guide show you how to set up your mobile app to enable 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.

You can download a sample project from GitHub.

Login Button

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

import FacebookLogin

class MyViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let loginButton = FBLoginButton(readPermissions: [ .publicProfile ])
        loginButton.center = view.center

        view.addSubview(loginButton)
    }

}

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 caches it in UserDefaults. 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:

override func viewDidLoad() {
    super.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 FBLoginButton use the readPermissions constructor to request additional read permissions.

You can initialize with a list of read permissions:

// Extend the code sample "1. Add Facebook Login Button Code"
// In your viewDidLoad method:
loginButton = FBLoginButton(
    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 FBLoginButton that conforms to the LoginButtonDelegate protocol.

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 FacebookLogin

class LoginManagerViewController: UIViewController {

    func loginManagerDidComplete(_ result: LoginResult) {
        let alertController: UIAlertController
        switch result {
        case .cancelled:
            alertController = UIAlertController(
                title: "Login Cancelled",
                message: "User cancelled login."
            )

        case .failed(let error):
            alertController = UIAlertController(
                title: "Login Fail",
                message: "Login failed with error \(error)"
            )

        case .success(let grantedPermissions, _, _):
            alertController = UIAlertController(
                title: "Login Success",
                message: "Login succeeded with granted permissions: \(grantedPermissions)"
            )
        }
        self.present(alertController, animated: true, completion: nil)
    }

    @IBAction private func loginWithReadPermissions() {
        let loginManager = LoginManager()
        loginManager.logIn(
            permissions: [.publicProfile, .userFriends],
            viewController: self
        ) { result in
            self.loginManagerDidComplete(result)
        }
    }

    @IBAction private func logOut() {
        let loginManager = LoginManager()
        loginManager.logOut()

        let alertController = UIAlertController(
            title: "Logout",
            message: "Logged out."
        )
        present(alertController, animated: true, completion: nil)
    }
}

Login Review

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

Login Review Guide