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

0
Shares
How to Integrate Twitter REST API in Swift to Make Interaction With Twitter Seamless
4.7 (93.33%) 3 votes

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 https://fabric.io/

2_1

Select apple logo to download the mac app.

It will start downloading Fabric-latest.zip

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

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

2_2

Open XCode from applications.

Select Create a new xcode project.

3_1

Select Single View Application and Click Next.

3_2

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

3_3

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 XCode.app available in Applications
  • This will open the Podfile with XCode

Default content of podfile will look as shown in image.

5_1

Add these to podfile.

  • pod ‘Fabric’
  • pod ‘TwitterKit’

Save and close the podfile.

5_2

$ 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.

6_1

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

6_2

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

7_1

Select your organization > Next

Select Install(Twitter) > Next

7_2

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

8_1

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

  • Fabric.with([Twitter.self])

8_2

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

Click Done.

8_3

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);
return
}

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)

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

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

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()]

composer.show(from: 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.

12_1

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

12_2

You will get alert with your username.

Tap compose tweet > Edit your tweet > Post

12_3

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.

 
0
Shares
 

Have an iOS App Idea in Mind? Let's Discuss!