Friday, February 3, 2012

Using The Twitter Framework in iOS 5

The twitter framework apple has added is a Christmas gift to all the iOS developers as twitter integration has now become a child's play. By using the twitter framework you may now post your stuff to twitter but a user only have to connect to Twitter with his account once and he/she can do this in the setting section of the iPhone or iPod and look for the twitter app settings. After that, apps have the possibility to post a tweet with the users account.

TWTweetComposeViewController class  in the twitter framework makes every thing possible if you want to tweet in the iOS 5 but if you want the twitter integration in the other devices then in that case you have to use the old methods of tweeting and in case if your application has support from iOS 4 to iOS 5 then in that case you need to perform a check and then tweet happily ever after . 

In this tutorial I’m going to show you how to use this class and start using the Twitter integration. You will be surprised how easy this class is to implement. Check the end of this article for a sample project you can download.

Step 1: Open Xcode and create an empty project add the ViewController subclass file in the project with Xib and create a view which looks something like the one given below.

Step 2: Select the .h file of your viewcontroller and create a functions to connect to the button, an IBOutlet to connect to the text field and one alert view object which will be used to notify the user regarding his tweets. Also add the twitter framework into your project, select the xcode project file and go to build phases and from their expand the link binary with library option.

Link Binary with library

Twitter Framework

 Given below is the code view for the .h file of the view controller

Step3: Select the .m file of your view controller and add the below code, i have divided the code blocks with the explanation for the same so that it will be easy for you to understand.

Code explanation: a) First we are checking whether the device supports the Twitter framework or not.

or you can also check for twitter account with the help of below code

b) Secondly we set the text for the content that we want to tweet with the help of the method setInitialText and then display the twitter composer modal view controller.

c) A user has to know whether his tweet has been posted or not so we will also check this with the help of TWTweetComposerViewControllerResult type and with the help of completionHandler property.
completionHandler: This is the handler that gets called when the user is done composing the tweet.

TWTweetComposerViewControllerResult: This is a enumerator with various values that determines whether a tweet is done successfully or has failed due to some problems like no internet connection or 3G connections etc.

Step 4: Once all the above code is understood by you and everything is working fine so far then its time you connected your IBOutlets and IBAction and then move to the AppDelegate.m file where you will add the twitter view into the iPhone window in the application did finish launching method.

Step 5: Run the application and start tweeting.

In case if their are no twitter account configured in your simulator you will get the below screen

You can download the code snipped here.

In case if you get some error which says

"_OBJC_CLASS_$ACAccountStore" referenced from objc-class-ref from TwitterViewController.o 

Then kindly add the accounts framework to fix it

You may also post images and URL Links with the help of the methods in the TWTweetComposeViewController

Image:  [twitterComposer addImage:SomeImagePresent_InBundleor_Camera];

URL: [twitterComposer addURL:someURL];

If you have any queries or if you think that i have missed out any important point then let me know via comments or mails, until then Happy iCoding and have a great Day.


  1. Hi. Nice post.
    I have a question.
    If I want to tweet any message directly without showing twitter screen to user, then how it would be possible?
    Thanks in advance

  2. Hi ,
    if we post image in tweeter thn how it would be ?

  3. @Aonymous: IT's given in the demo code in comments, download it and check it out.

  4. This comment has been removed by the author.

  5. Great tutorial :) Instead of iPhone Settings,If i want to force a user to enter his credentials of twitter with same login window as FbGraphApi then?How can we do this?