iOS

Facebook login

11min

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:

1 - Facebook Set up

To start using Facebook functions, you need to:

  1. Go to the Facebook Developer Website and create an account and an app.
  2. Add your application’s Facebook Application ID on your Parse application’s settings page.
  3. 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

  1. Go to your App dashboard at Back4App Website and click on Server Settings.
  2. Find the “Facebook Login” block and click on Settings. The “Facebook Login” block looks like this:
Document image
ďťż

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

  1. Add the following to your application:didFinishLaunchingWithOptions: method, after you’ve initialized the Parse SDK:
Swift
ďťż

2. Add the following handlers in your app delegate:

Swift
ďťż

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:

Swift
ďťż

When this code is run, the following happens:

  1. The user is shown the Facebook login dialog.
  2. The user authenticates via Facebook, and your app receives a callback using handleOpenURLďťż
  3. 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.
  4. Your code block is called with the user.
  5. 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:]:ďťż

Swift
ďťż

5 - Linking

If you want to associate an existing PFUser to a Facebook account, you can link it like so:

Swift
ďťż

6 - UnLinking

If you want to unlink Facebook from a user, simply do this:

Swift
ďťż

ďťż