iOS 5 : Twitter Framework – Part 1 – Compose a Tweet with TWTweetComposeViewController

This is the first post of two on how to work with the new Twitter framework in an iOS 5 app. This post will cover the basics to create and display a dialog for composing a tweet. Part 2 in this series can be found here.

The primary controller for working with Twitter in iOS 5 is TWTweetComposeViewController. This object handles all the UI related work to display and manage input – you can pre-populate the initial text, add an image as well as a URL. The code is straight forward to get things rolling:

#import <Twitter/Twitter.h>
// Create the view controller
TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];
// Optional: set an image, url and initial text
[twitter addImage:[UIImage imageNamed:@"iOSDevTips.png"]];
[twitter addURL:[NSURL URLWithString:[NSString stringWithString:@""]]];
[twitter setInitialText:@"Tweet from iOS 5 app using the Twitter framework."];
// Show the controller
[self presentModalViewController:twitter animated:YES];
// Called when the tweet dialog has been closed
twitter.completionHandler = ^(TWTweetComposeViewControllerResult result) 
  NSString *title = @"Tweet Status";
  NSString *msg; 
  if (result == TWTweetComposeViewControllerResultCancelled)
    msg = @"Tweet compostion was canceled.";
  else if (result == TWTweetComposeViewControllerResultDone)
    msg = @"Tweet composition completed.";
  // Show alert to see how things went...
  UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil];
  [alertView show];
  // Dismiss the controller
  [self dismissModalViewControllerAnimated:YES];

Notice the Objective-C block (more on blocks here) that is defined for the completion handler, this is called once the user has finished composing the tweet.

Before running the code you’ll need to configure a Twitter account. If you skip this step you’ll see the dialog on the left below. The image on the right is the settings screen to configure an account. Also, you will need to add the Twitter framework to your Xcode project before compiling.

The screenshot below shows the Twitter view controller poplated with the content shown above.

Below is a screenshot from the twitter page showing how the tweet above looks at

That’s all you need to get started for basic twitter integration. In the next post I’ll go into more detail including a look at the ACAccountStore for managing accounts and TWRequest for sending HTTP requests to Twitter.

  1. Looks good.

    Just briefly looking over it – is the twitter object getting released?

    Thanks for the great post!

  2. Hello, what is interesting about TWTweetComposeViewController is if you have multiple accounts how does it know which one to use? Or does it just pick the first one and if your sending a private message using “d screen_name text” how would that work if you don’t know which account it is using?

    Thanks a lot,


    • If you’re in portrait orientation the TWTweetComposeViewController will have a new field if you have set up more than one account, like

      Cancel ——Tweet——Send
      Blah Blah Blah

      If you tap on the account name a picker with all your accounts will appear, letting you decide which one you will want to publish the tweet with.

  3. You alloc/init -> TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];

    but it never gets release, It should be released in the completionHandler block!

    also you might want to check for if the class of twitter exists for backward compatibility…

    Class tweeterClassExists = NSClassFromString(@”TWTweetComposeViewController”);

    if(tweeterClassExists != nil) { // check for Twitter integration
    … it exists do your twitter code here …

    • Actually, if you’re on iOS 5 and you enable ARC (Automatic Reference Counting), you don’t need to release anything anymore. The compiler handles all the retaining and releasing.

  4. Hi,

    How can we reply to a tweet using this api?

    Thanks a lot

  5. Hi All,

    is there a way to post picture that I just took from the camera? I think yes but I am struggling with that…I have all code already for taking the picture and the tweet sheet…however I can’t understand how to tell the xcode to grab my existing UIMAGEVIEW to the tweet….anyone?

  6. TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];
    NSString *format = @”“%@” %@ /via @DesignSceneApp”;
    NSString *message = [NSString stringWithFormat:format, title, url]
    NSUInteger idx = title.length;
    while (![twitter setInitialText:message]) {
    idx -= 5;
    if (idx > 5) {
    message = [NSString stringWithFormat:format,
    [NSString stringWithFormat:@”%@…”, [title substringToIndex:idx]],
    } else {
    // Give up on the title.
    message = [NSString stringWithFormat:@”%@ /via @DesignSceneApp”, url];
    [twitter setInitialText:message];

    [self presentViewController:twitter animated:YES completion:nil];

Comments are closed.