How to Integrate Twitter REST API in Swift to Make Interaction With Twitter Seamless

The new Twitter kit is designed to make interacting with Twitter much more efficient. The Twitter REST API offers a way for developers to make using Twitter seamless, and it is used to make authenticated Twitter API requests. Whether it’s logging someone in or tweeting from your app, the new Twitter kit makes performing these tasks efficient.

Though, the Twitter REST API can be accessed manually, but we always prefer using the convenience methods. In fact, in this iphone app tutorial, we’ll learn how to integrate Twitter REST API in swift.

Let’s Get Started

First, Login/Signup for


Select apple logo to download the mac app.

It will start downloading

Once the download is completed, unzip the file and move the to your mac Applications.

Now open from applications and Login using the account you created.


Open XCode from applications.

Select Create a new xcode project.


Select Single View Application and Click Next.


Enter Product name “TwitterKitDemoApp”, Language: “Swift”, Devices: “iPhone”, and Click Next.


Select location to save the project and Hit “Create”.

Install CocoaPods

Open terminal and enter the following commands.

$ sugo gem install cocoapods

  • Enter password when requrested

$ pod setup –verbose

  • This will take some time to complete

$ cd ~path/to/projectDirectory

  • E.g. cd /Users/hitendramac/Documents/Hitendra/Projects/Other/TwitterKitDemoApp

$ pod init

  • This will create “Podfile” into project directory

$ open -a Xcode Podfile

  • Make sure available in Applications
  • This will open the Podfile with XCode

Default content of podfile will look as shown in image.


Add these to podfile.

  • pod ‘Fabric’
  • pod ‘TwitterKit’

Save and close the podfile.


$ pod install

  • This will take some time to complete.
  • You will get output as shown in the image.

This will create “TwitterKit.xcworkspace” and required stuff in the project directory.


Close the xcode project “TwitterKit” and open the “TwitterKit.xcworkspace”.


Now onwards we will use “TwitterKit.xcworkspace”.

Download pods have been integrated with the TwitterKit.xcworkspace as a dependency.

Add Application to Fabric

Open fabric app in which you have logged in.

Select click “+New App” button.

Select TwitterKitDemoApp.xcworkspace > Next


Select your organization > Next

Select Install(Twitter) > Next


Select Podfile option.

Skip edit your pod file step (we have already completed this step)

Follow this link or this link to add run script to your project.

Once you add the run script Build the project [cmd + B].

Code Setup

Goto the AppDelegate.swift

Add two Import statements

  • import Fabric
  • import TwitterKit


Add this code inside the application(_:didFinishLaunchingWithOptions) method.

  • Fabric.with([Twitter.self])


Once you are done with the Appdelegate.swift run the app [cmd+R]

Click Done.


Goto the ViewController.swift

Add a Import statements

import TwitterKit

Add this code inside the viewDidLoad() method.

//login button setup
let logInButton = TWTRLogInButton { (session, error) in
guard let unwrappedSession = session else {//early return if error
print("Login error: %@", error!.localizedDescription);

let alert = UIAlertController(title: "Logged In",
message: "User \(unwrappedSession.userName) has logged in",
preferredStyle: UIAlertControllerStyle.alert
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)

} = CGPoint(x:, y: 25 + logInButton.frame.size.height)

//compose tweet button setup
let button = UIButton(type: .system)
button.setTitle("Compose tweet", for: [])
button.sizeToFit() =
button.addTarget(self, action: #selector(compose), for: [.touchUpInside])

Add this method compose() inside the ViewController.swift

func compose() {
// Swift
let composer = TWTRComposer()
composer.setText("just setting up my Fabric")
//you can also set the image here [composet.setImage()]
//you can also set the URL here [composet.setURL()] self) { result in
if (result == TWTRComposerResult.cancelled) {
print("Tweet composition cancelled")
else {
print("Sending tweet!")

Run the app.

Tap on “Log in with Twitter” button.


You will be redirected to twitter login flow > complete the login flow.


You will get alert with your username.

Tap compose tweet > Edit your tweet > Post


Voila.! Your tweet has been posted!

While the function does look like basic, but it offers app users to directly publish media and tweet from your mobile app. And, to make sure that it works correctly, hire iPhone app development company to implement the REST API for your mobile app.

Author Bio
Hitesh Trivedi
Hitesh Trivedi
Designation: iOS Team Lead

Hitesh Trivedi is an iOS Team Lead at Space-O Technologies. He has over 10 years of experience in iOS app development. He has guided to develop over 100 iPhone apps with unique features and functionalities. He has special expertise in Swift and Objective-C.