Social networking has become part of our life, indeed we are posting every part of our life in one or the other sites. So, as a developer you need to give the ability to share from our App.
Today we are going to discuss about how to tweet using Twitter API in your App.

Introduction
In iOS 5, Apple has done a great job by introducing the Twitter framework by which many integration problems such as API access, session settings etc were prevented.

Create a new Single View Application with Automatic Reference Counting

To use the Twitter framework, we need to include the framework

  1. 1. Click on the Target (In this case TwitterDemo)
  2. 2. Then access the Frameworks and Libraries section and click on “+” to add a new framework. (observer Figure 1)

To use the framework we have just added, we need to include the framework in our header file as shown below.

#include <Twitter/Twitter.h>

TWTweetSheetViewController is responsible to present a view to compose a Tweet.

Now drag and drop a Button on to the view, as shown in the below figure.

Write down the following code in the method definition.

- (IBAction)tweet:(id)sender {
    TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];

    // Set the initial tweet text. See the framework for additional properties that can be set.
    [tweetViewController setInitialText:[NSString stringWithFormat:@"%@", Tweet_Message]];

    [tweetViewController setCompletionHandler:^(TWTweetComposeViewControllerResult result) {
        switch (result) {
            case TWTweetComposeViewControllerResultCancelled:
                // The cancel button was tapped.
                NSLog(@"Tweet cancelled.");
                break;
            case TWTweetComposeViewControllerResultDone:
                // The tweet was sent.
                NSLog(@"Tweet done.");
                break;
            default:
                break;
        }
        [self dismissModalViewControllerAnimated:YES];
    }];

    // Present the tweet composition view controller modally.
    [self presentModalViewController:tweetViewController animated:YES];
}

Now run the project, If you did not set the Twitter account in the Simulator/Device you will be getting an alert as shown in the below figure.

Download Source for the demo project.
If you have any queries, don’t hesitate to comment ;)

About the Author: pradyumnadoddala

Passionate iOS geek experienced in developing apps for iPhone and iPad dealing with Conceptual, Data driven Apps with Online Offline sync, CRM and CLM Mobile Solutions, rich Animated UI Apps, 2D Games using cocos2d, Social networking and Entertainment Apps.