iOS

Twitter login

8min

Add Twitter login to your iOS App using Swift

Introduction

This section explains how you can create an app with user registration using Twitter Login and Parse Server core features through Back4App.

Prerequisites

To complete this quickstart, you need:

1 - Twitter Set up

To start using Twitter functions, you need to:

  1. Go to Twitter Application Management Website, sign in with a Twitter account and click on Create New App.
  2. Add your applicationā€™s Twitter consumer key on your Parse applicationā€™s settings page.
  3. When asked to specify a ā€œCallback URLā€ for your Twitter app, please insert a valid URL likehttp://twitter-oauth.callback. This value will not be used by your iOS or Android application, but is necessary in order to enable authentication through Twitter.
  4. Add the Accounts.framework and Social.framework libraries to your Xcode project.
  5. Add the following where you initialize the Parse SDK, such as inapplication:didFinishLaunchingWithOptions:
Swift
ļ»æ

2 - Login and SignUP

ļ»æPFTwitterUtils provides a way to allow your PFUsers to log in or sign up through Twitter. This is accomplished using the logInWithBlock or logInWithTarget messages:

Swift
ļ»æ

When this code is run, the following happens:

  1. The user is shown the Twitter login dialog.
  2. The user authenticates via Twitter, and your app receives a callback.
  3. Our SDK receives the Twitter data and saves it to a PFUser. If itā€™s a new user based on the Twitter handle, then that user is created.
  4. Yourblock is called with the user.

3 - Twitter Linking

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

Swift
ļ»æ

The steps that happen when linking are very similar to log in. The difference is that on successful login, the existing PFUser is updated with the Twitter information. Future logins via Twitter will now log the user into their existing account.

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

Swift
ļ»æ

6 - Twitter API calls

Our SDK provides a straightforward way to sign your API HTTP requests to the Twitter REST API when your app has a Twitter-linked PFUser. To make a request through our API, you can use the PF_Twitter singleton provided by PFTwitterUtils:

Swift
ļ»æ

ļ»æ