Facebook login
Add Facebook login to your iOS App using Swift tutorial
Introduction
This section explains how you can create an app with user registration using Facebook Login and Parse Server core features through Back4App.
Prerequisites
To complete this quickstart, you need:
- An app created at Back4App.
- An iOS app connected to Back4App.
- Note: Follow the Install Parse SDK (ObjC) Tutorial to create an Xcode Project connected to Back4App.
1 - Facebook Set up
To start using Facebook functions, you need to:
- Add your application’s Facebook Application ID on your Parse application’s settings page.
- Follow Facebook’s instructions for getting started with the Facebook SDK to create an app linked to the Facebook SDK.
2 - Link your Facebook App with Back4App
- Find the “Facebook Login” block and click on Settings. The “Facebook Login” block looks like this:

3. Go back to your XCode Project, open your info.plist copy the code from Facebook Configuration, Step 4a, item 2, and paste it in the <dict>...</dict> part of your info.plist.
4. In order to use a dialog box from Facebook, also copy and paste the code from section 4a, item 3 into your info.plist file.
5. Save
3 - Setting up your App
- Add the following to your application:didFinishLaunchingWithOptions: method, after you’ve initialized the Parse SDK:
1 import FBSDKCoreKit
2 import Parse
3
4 // AppDelegate.swift
5 func application(application: UIApplicatiofunc application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
6 // Initialize Parse.
7 let parseConfig = ParseClientConfiguration {
8 $0.applicationId = "parseAppId"
9 $0.clientKey = "parseClientKey"
10 $0.server = "parseServerUrlString"
11 }
12 Parse.initialize(with: parseConfig)
13 PFFacebookUtils.initializeFacebook(applicationLaunchOptions: launchOptions)
14 }2. Add the following handlers in your app delegate:
1 func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
2
3 return FBSDKApplicationDelegate.sharedInstance().application(
4 application,
5 open: url,
6 sourceApplication: sourceApplication,
7 annotation: annotation
8 )
9
10 }
11
12 func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
13
14 return FBSDKApplicationDelegate.sharedInstance().application(
15 app,
16 open: url,
17 sourceApplication: options[.sourceApplication] as? String,
18 annotation: options[.annotation]
19 )
20
21 }
22
23 //Make sure it isn't already declared in the app delegate (possible redefinition of func error)
24 func applicationDidBecomeActive(_ application: UIApplication) {
25 FBSDKAppEvents.activateApp()
26 }4 - Log in & Sign Up
PFUser provides a way to allow your users to log in or sign up through Facebook. This is done by using the logInInBackgroundWithReadPermissions method like so:
1 PFFacebookUtils.logInInBackground(withReadPermissions: permissions) {
2 (user: PFUser?, error: Error?) in
3 if let user = user {
4 if user.isNew {
5 print("User signed up and logged in through Facebook!")
6 } else {
7 print("User logged in through Facebook!")
8 }
9 } else {
10 print("Uh oh. The user cancelled the Facebook login.")
11 }
12 }When this code is run, the following happens:
- The user is shown the Facebook login dialog.
- The user authenticates via Facebook, and your app receives a callback using handleOpenURL
- Our SDK receives the user’s Facebook access data and saves it to a PFUser. If no PFUser exists with the same Facebook ID, then a new PFUser is created.
- Your code block is called with the user.
- The current user reference will be updated to this user.
The permissions argument is an array of strings that specifies what permissions your app requires from the Facebook user. These permissions must only include read permissions.
To acquire publishing permissions for a user so that your app can, for example, post status updates on their behalf, you must call [PFFacebookUtils logInInBackgroundWithPublishPermissions:]:
1 PFFacebookUtils.logInInBackgroundWithPublishPermissions(["publish_actions"], {
2 (user: PFUser?, error: NSError?) -> Void in
3 if user != nil {
4 // Your app now has publishing permissions for the user
5 }
6 })5 - Linking
If you want to associate an existing PFUser to a Facebook account, you can link it like so:
1 if !PFFacebookUtils.isLinkedWithUser(user) {
2 PFFacebookUtils.linkUserInBackground(user, withReadPermissions: nil, {
3 (succeeded: Bool?, error: NSError?) -> Void in
4 if succeeded {
5 print("Woohoo, the user is linked with Facebook!")
6 }
7 })
8 }6 - UnLinking
If you want to unlink Facebook from a user, simply do this:
1 PFFacebookUtils.unlinkUserInBackground(user, {
2 (succeeded: Bool?, error: NSError?) -> Void in
3 if succeeded {
4 print("The user is no longer associated with their Facebook account.")
5 }
6 })