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 tutorial, you need:
- XCode.
- An app created on Back4App.
- Note: Follow the New Parse App Tutorial to learn how to create a Parse App on Back4App.
- An iOS app connected to Back4app.
- Note: Follow the Install Parse SDK Tutorial to create an XCode Project connected to Back4App.
Step 1 - Twitter Set up
To start using Twitter functions, you need to:
- Go to Twitter Application Management Website, sign in with a Twitter account and click on
Create New App
. - Add your application’s Twitter consumer key on your Parse application’s settings page.
- When asked to specify a “Callback URL” for your Twitter app, please insert a valid URL like
http://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. - Add the
Accounts.framework
andSocial.framework
libraries to your Xcode project. - Add the following where you initialize the Parse SDK, such as in
application:didFinishLaunchingWithOptions
:
1
PFTwitterUtils.initializeWithConsumerKey("YOUR CONSUMER KEY", consumerSecret:"YOUR CONSUMER SECRET")
Step 2 - Login and SingUP
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:
1
2
3
4
5
6
7
8
9
10
11
12
PFTwitterUtils.logInWithBlock {
(user: PFUser?, error: NSError?) -> Void in
if let user = user {
if user.isNew {
print("User signed up and logged in with Twitter!")
} else {
print("User logged in with Twitter!")
}
} else {
print("Uh oh. The user cancelled the Twitter login.")
}
}
When this code is run, the following happens:
- The user is shown the Twitter login dialog.
- The user authenticates via Twitter, and your app receives a callback.
- 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. - Your
block
is called with the user.
Step 3 - Twitter Linking
If you want to associate an existing PFUser
with a Twitter account, you can link it like so:
1
2
3
4
5
6
7
8
if !PFTwitterUtils.isLinkedWithUser(user) {
PFTwitterUtils.linkUser(user, {
(succeeded: Bool?, error: NSError?) -> Void in
if PFTwitterUtils.isLinkedWithUser(user) {
print("Woohoo, user logged in with Twitter!")
}
})
}
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:
1
2
3
4
5
6
PFTwitterUtils.unlinkUserInBackground(user, {
(succeeded: Bool?, error: NSError?) -> Void in
if error == nil && succeeded {
print("The user is no longer associated with their Twitter account.")
}
})
Step 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
:
1
2
3
4
5
6
7
8
let verify = NSURL(string: "https://api.twitter.com/1.1/account/verify_credentials.json")
var request = NSMutableURLRequest(URL: verify!)
PFTwitterUtils.twitter()!.signRequest(request)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
// Check for error
// Data will contain the response data
}
task.resume()