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

Mon, Oct 17

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:@"http://iosdevelopertips.com/"]]];
[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 twitter.com/iOSDevTips.

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.

8 comments

Looks good.

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

Thanks for the great post!

by bruce on Nov 1, 2011. #

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,

Kenneth

by Kenneth Lewis on Nov 8, 2011. #

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
@account
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.

by Rodrigo on Nov 15, 2011. #

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 …
}

by Dimitris on Nov 25, 2011. #

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.

by Henrik Hodne on Dec 12, 2011. #

Hi,

How can we reply to a tweet using this api?

Thanks a lot

by meo on Mar 14, 2012. #

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?

by Rheder Silva on Oct 15, 2012. #

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]],
url
];
} else {
// Give up on the title.
message = [NSString stringWithFormat:@"%@ /via @DesignSceneApp", url];
[twitter setInitialText:message];
break;
}
}

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

by Tamilarasn on Nov 28, 2012. #