Site icon Tutorial

Facebook Login in Swift

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)

}

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.

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.

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;

myLoginButton.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.

Exit mobile version