You can allow people to log into your iOS app with Facbook 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. You can also check the current login status and ask for permission on a subset of a person's data.
You will need to integrate the Facebook SDK for iOS into your app. Learn more.
Add the following import
statement:
import FacebookLogin
Add the code to display the Facebook login button.
let loginButton = FBLoginButton() loginButton.center = view.center view.addSubview(loginButton)
Your app can only have one person logged in at a time. We represent each person logged into your app with the AccessToken currentAccessToken
.
The LoginManager
sets this token for you and when it sets currentAccessToken it also automatically writes it to a keychain cache.
The AccessToken
contains userID
which you can use to identify the user.
You should update your view controller to check for an existing token at load. This avoids unnecessary showing the login flow again if someone already granted permissions to your app:
let token = AccessToken.current, if !token.isExpired { // User is logged in, do work such as go to next view controller. }
To request additional read permissions, set the readPermissions
property on the loginButton
object.
// Swift // // Extend the code sample from 6a. Add Facebook Login to Your Code // Add to your viewDidLoad method: loginButton.permissions = ["public_profile", "email"]