Get Started Developing with iOS 5’s Twitter Framework

&c=” border=”0″ alt=”” />

Advertise here via BSA

As you know by now, iOS 5 brought built-in Twitter functionality to the platform, and you no longer need to master third-party frameworks to use Twitter functionality in your apps. The purpose of this tutorial is to get you up and running with this new core framework and to teach you how to tweet predefined text from within your own app as well as how to send the tweet and handle errors.

Part One: Creating the Method

1. First, we need to set up our project, link with the framework and create the actual method that will send your tweet. To do this open up the latest version of Xcode and create a New Project (New – Project…) and select Single View Application. Press Next and give your project a name. In this case I’ll call it Tweeted and you can keep the company identifier the same and the class prefix blank along with all the checkboxes unchecked, and also make sure that device family is the iPhone. Press Next and save your project.

2. Now we have created our project, we’re going to link the Twitter framework with our project. To do this, click the name of your project at the top of the Project Navigation and along the top tabs, click Build Phases. Expand Link Binary With Libraries and press the small “+” in the bottom left.

3. Now, search for “Twitter” and then select the Twitter framework. Press Add. This will add the Twitter framework so you can use it within your project.

Adding the Framework

Adding the Framework

4. Now with our linked framework, open ViewController.h from within the Project Navigator and add the following line below #import:

#import <Twitter/Twitter.h>

5. Now, in the same file (ViewController.h), we need to define the method we will call to send a tweet, to do this add the following line below the @interface line:


6. Now, in the Project Navigator, open up ViewController.m; here we’re going to actually create the method, so under the final curly brace of the -(void)viewDidLoad method, add the following code:

-(IBAction)sendTweet:(id)sender {
TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];
[twitter setInitialText:@"Eh up, tweeting directly from within iOS 5."];
[self presentModalViewController:twitter animated:YES];

twitter.completionHandler = ^(TWTweetComposeViewControllerResult result) {
NSString *title = @"Tweet";
NSString *msg;

if (result == TWTweetComposeViewControllerResultCancelled)
msg = @"You bailed on your tweet...";
else if (result == TWTweetComposeViewControllerResultDone)
msg = @"Hurray! Your tweet was tweeted!";

UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil];
[alertView show];

[self dismissModalViewControllerAnimated:YES];


7. In the above code, you can change the contents of the text within @”…” with the text you would like. For example, you can change @”Eh up, tweeting directly from within iOS 5.” to @”Hello, this is my tweet”. In the above code, we’ve also added some text that will display if the tweet is successful (Hurray! Your tweet was tweeted) and if the tweet doesn’t send, for example, the user presses the cancel button (You bailed on your tweet) – feel free to change this text.

Part Two: Connecting it to Your Interface

1. So far, we have done all the backend work to tweet from within our own application, we’ve added the Twitter framework as well as created the method that will run to send a tweet and now we need to actually hook it up to a button.

2. In the Project Navigator, open up ViewController.xib and open up the Object Library (View – Utilities – Show Object Library) and it will appear in the bottom right of Xcode’s window.

3. Within the Object Library, search for “button” and you’ll be given a variety of results, for this we want a Round Rectangular Button, so drag this to your view. Once you’ve dragged the button to the view, you can now change the size of it and double click it to add some text.

4. On the left of the window, click on the box icon (the File’s Owner) and open up the Connections Inspector (View – Utilities – Show Connections Inspector) and now you’ll see a list of methods associated with your project. Select the circle to the right of the Connections Inspector next to the sendTweet method and drag it to the button you dragged earlier. Release the mouse and click Touch Up Inside.

Making the Connection

Making the Connection

5. Now, if you run your application (Cmd + R), you’ll find that your application runs and when you press the button, a tweet dialogue should display.

Tweet Dialogue

Tweet Dialogue



Part Three (Optional): Configuring the iOS Simulator

1. You can configure the iPhone Simulator with your own Twitter account. With your application running, press the home button on the simulator and open up the Settings app and select Twitter.

2. Here, you can add your own Twitter account so that when you run the application we’ve created, it will tweet successfully from your own account.

Twitter Accounts

Twitter Accounts

The purpose of this tutorial was to help you get familiar with iOS 5’s new Twitter integration. Be aware to run your app only on an iOS 5 device otherwise your app will crash!

Click here to download the source code.

Fuel Your Coding

Leave a Comment