Categories: Swift Programming

Twitter Integration with Fabric in iOS

Hello everyone!

I thought that I should take some time out to write this basic twitter integration tutorial. Its pretty simple.

Before we start, there is something we need to know.

What is Fabric?
Its an SDK which can be used to integrate different Kits to your app. Kits like Twitter and Crashlytics, etc. Read more.

Lets get started.

Login to your Fabric account and on the side menu bar click downloads.

Click on Xcode

Then Click download. After the download finishes Unzip it and double-click open the Fabric.app.

After Logging in to your Fabric account. You’ll be redirected to a list of your projects. I have already created a project named Twitter Integration so I’m gonna select it then click Next.

Select the Kit you want to install.

Then Click Agree.

Open Up your Project.

Then Click Your Project -> Build Phases. Then Click Plus button then “New Run Script Phase”.

Copy the Script provided by Fabric in the section and build your project once.

Drag the briefcase into your project and make sure that Copy Items if needed is unchecked.

After that open your app delegate and import Fabric, and TwitterKit.

import Fabric
import TwitterKit

And in your didFinishLaunchingWithOptions.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
       
        Fabric.with([Twitter.self])

        return true
    }

Click Next.

Using Default button provided by the Kit

In your viewDidLoad.

//Declare loginInButton outside viewDidLoad
 logInButton = TWTRLogInButton { (session, error) in
            if let unwrappedSession = session {
                let client = TWTRAPIClient()
                client.loadUserWithID((unwrappedSession.userID), completion: { (user, error) -> Void in
                    print("UserName: \(unwrappedSession.userName)")
                    print("Name: \(user!.name)")
                    print("Image Url: \(user!.profileImageURL)")
                })

                self.logInButton.hidden = true
                self.logout.hidden = false
                self.customLogin.hidden = true

            } else {
                NSLog("Login error: %@", error!.localizedDescription);
            }
        }
        
        logInButton.center = self.view.center
        self.view.addSubview(logInButton)

Build and Run.

Using Button with Custom Image

Drag a button in your ViewController and create an @IBOutlet and @IBAction. In your @IBAction copy the following code.

  Twitter.sharedInstance().logInWithCompletion {
            (session, error) -> Void in
            if (session != nil) {
                print("signed in as \(session!.userName)")
                self.logout.hidden = false
                self.logInButton.hidden = true
                  self.customLogin.hidden = true
                let client = TWTRAPIClient()
                client.loadUserWithID((session?.userID)!, completion: { (user, error) -> Void in
                    print("Name: \(user!.name)")
                    print("Image Url: \(user!.profileImageURL)")
                })
                        
                    }   
 }

For logging out functionality, in your logout button, do the following.

  let store = Twitter.sharedInstance().sessionStore
        
        if let userID = store.session()?.userID {
            store.logOutUserID(userID)
            print("Logged Out!")
            
        }

Running the project.

You can find this project on my Github.
If you have any question please leave a comment.

Have a great day.

Aaqib Hussain

Aaqib is an enthusiastic programmer with the love of Swift and anything that looks like Swift i.e Kotlin. He loves writing code in Swift, and exploring new technology and platforms. He likes to listen to old music. When he is not writing code, he's probably spend his time watching movies, tv-shows or anime, or either doing some research for writing the next article. He started Kode Snippets in 2015.

View Comments

Recent Posts

Things to know when moving to Germany

This article covers some important things you must know when you are considering a move…

3 years ago

Unit Testing in Android for Dummies

What is Unit Testing? In its simplest term, unit testing is testing a small piece…

4 years ago

Factory Design Pattern

In this article, you will learn about a type of Creational Design Pattern which is…

5 years ago

Creating Target specific Theme in iOS

In this tutorial, you will go through the use of targets to achieve two separate…

5 years ago

Facade Design Pattern

In this article, you will learn about a type of Structural Design Pattern which is…

5 years ago

Singleton Design Pattern

In this article you will learn about a type of Creational Design Pattern which is…

5 years ago