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.
This article covers some important things you must know when you are considering a move…
What is Unit Testing? In its simplest term, unit testing is testing a small piece…
In this article, you will learn about a type of Creational Design Pattern which is…
In this tutorial, you will go through the use of targets to achieve two separate…
In this article, you will learn about a type of Structural Design Pattern which is…
In this article you will learn about a type of Creational Design Pattern which is…
View Comments
that was great can you please answer my question here
http://stackoverflow.com/questions/37435459/twitter-single-sign-on-ios-using-swift