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. Click on the Target (In this case TwitterDemo)
- 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








The flow of this sample is missing an important step. In addition to the call that brings up the System No Account alert, you also need to make sure you have permission from the user. Checking that will bring up the system alert that lets the user Allow/Don’t Allow the App.
This is important, because if the user changes the App permission, this is a way that the system can re-enable the App without the user going to settings.
To find out more, look at the Account framework docs.
Regards
Maurice Sharp
Hey Maurice, thanks for your suggestion. As this post is just an introduction, which means you will be knowing the basic way to Tweet here. The thing about request access comes up when you are doing a Customised Tweet, which will be done by using Account Framework (In this case user doesnt have any control over the Tweet process)..
In this example the User will have control over the tweet so theres so need to ask for the access..
Regards,
Pradyumna
Twitter