Twitter App Code for iOS 5

Coding an application with Twitter API use to be a lot of mess prior to iOS 5. There was no inbuilt support from Apple side. For even basic message tweeting, you had to integrate third party libraries like SAOAuth and  MGTwitterEngine.

Apple has realized Twitter is very important functionality for any App to proliferate in Social arena. Twitter being Simple and open source project is perfect choice for Apple to integrate in iOS 5.

With Twitter APi integration you can let your friends know when you scored highest in the game, when you clicked that nice photograph or when you have a website URL for reference.

Twitter is now very integral part of iOS 5 Architecture. Lets see how to build a basic Twitter Client App for Apple iOS 5 API without any frills.


Step 1:  Create new Xcode Project as Single View Application



Step 2: Add Twitter Library to Project from Build Phases




Step 3: Go to Story board and design the Twitter App

Lets create Two UITextFields and UILabels to accept the message and URL details celebrex price. Add a button and update the title to Tweet Message. change the background color to light gray. Change the keyboard type of second TextField to URL. Set the return key to Done.


Step 4: Add the IBOutlets and IBActions in ViewController.h

//  ViewController.h
//  TwitterAppiOS5
//  Created by Deepak Keswani on 15/04/12.
//  Copyright (c) 2012 D-Kay Consultancy. All rights reserved.


@interface ViewController : UIViewController {
    IBOutlet UITextField *message;
    IBOutlet UITextField *website;

- (IBAction) tweetMessage;
- (IBAction) closeKeyboard;


Step 5: According to header declaration add the methods in ViewController.m file

Add the implementation of these two methods. TweetMessage method instantiates the TWTweetComposerViewController and forms the message. Later a URL is attached to message. Before actually sending the message Controller verifies if Tweet Message can be sent. User can type the message and URL and click on Tweet Message to send the Tweet Message.

- (IBAction)tweetMessage {

    TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc]init];
    [twitter setInitialText:message.text];
    [twitter addURL:[NSURL URLWithString:website.text]];
    if ([TWTweetComposeViewController canSendTweet] ) {
        [self presentViewController:twitter animated:YES completion:nil ];

    }else {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Unable to tweet" message:@"This only works with Twitter configured iOS 5" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles: nil];
        [alertView show];
    twitter.completionHandler = ^(TWTweetComposeViewControllerResult res) {
        if(res == TWTweetComposeViewControllerResultDone) {
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Tweeet Succeeded" message:@"Message Sent Successfully" delegate:self cancelButtonTitle:@"Close" otherButtonTitles: nil];
            [alertView show];
        }else {
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Tweet Failed" message:@"Message sending Failed, Try again Later..." delegate:self cancelButtonTitle:@"Close" otherButtonTitles: nil];
            [alertView show];

- (IBAction) closeKeyboard {
    [self dismissModalViewControllerAnimated:YES];

Step 6: Create the connections between the display objects and methods.

Finally once all the connections are done, your screen should look like this as below:

Step 7: Build and Run the project to see the Twitter App for iOS 5 Platform.

Before you run this App in Simulator, please go to settings–>Twitter and save your twitter handle with password.

If twitter is not configured on simulator, this app will mount in Simulator, but you wont be able to send any Twitter Message.

You can also test the application by connecting your registered device with Mac and choose your device in Xcode for Build and Deployment. This is how it appears on Simulator Environment.


On Actual device, you get an intermediate screen for confirmation like this.


On Success of message sending you get an alert window like this.

Final output that you can see in your twitter profile looks like this.



You can also download this code from download section of website or from my GitHub source:


About Deepak Keswani 98 Articles
Developing Applications for Computers since 1995 :)

1 Comment

Leave a Reply

Your email address will not be published.


This site uses Akismet to reduce spam. Learn how your comment data is processed.