Translate

Thursday, May 24, 2012

Using The Twitter Framework in iOS 5

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







download link :  https://github.com/downloads/KRadix/CommonUtility/TwitterFrameworkDemo.zip

No comments:

Post a Comment