The Wayback Machine - https://web.archive.org/web/20131017221120/https://dev.twitter.com/docs/ios/using-tweet-sheet

Using the Tweet Sheet

Updated on Tue, 2013-10-08 17:43

Overview

remoteimage

The Tweet Sheet

The Social framework includes a powerful all-in-one class for composing Tweets called SLComposeViewController, also known as the "Tweet Sheet". The Tweet Sheet provides multiple mechanisms that enhance the user experience that previously required developer support to implement:

  • The UI of the sheet is handled for you
  • You can set the initial text of the Tweet, attach images, and attach URLs
  • The Tweet Sheet handles multiple accounts seamlessly
  • The controller autocompletes usernames as the user composes the Tweet
  • It handles counting characters to ensure the 140 character limit

iOS5 Support

The Tweet Sheet equivalent in iOS5 is called TWTweetComposeViewController and it is available in the Twitter.framework.

How do I use the Tweet Sheet in my application?

Invoking the Tweet Sheet in your application is quite simple. First, you should ensure that you have added the Social framework to your project as described in Adding the Social framework. Once you have added the framework to your project, using the Tweet Sheet is as simple as:

  1. Initialize a Tweet Sheet
  2. Configure the initial text, images, and URLs that you would like to set (if any)
  3. Configure the result handler
  4. Display the Tweet Sheet using -[presentViewController:animated:completion:]

Code Example

Below is an example of the above steps. Note that the methods for setting initial content respond with Boolean values; this allows you, the developer, to not have to worry about the current count of characters in the body of the Tweet that you are initializing. If the method returns YES, there was enough room to add the content. If the method returns NO, the content you attempted to add would result in a Tweet longer than 140 characters. The logic for character counting also takes into effect the current number of characters required for t.co URL wrapping.

Note

This example utilizes Automatic Reference Counting (ARC) and line breaks are added for clarity

Warning

In iOS7+, you should not dismiss the Tweet Sheet from within the completionHandler.

  1. - (void)showTweetSheet
  2. {
  3.   //  Create an instance of the Tweet Sheet
  4.   SLComposeViewController *tweetSheet = [SLComposeViewController
  5.                                          composeViewControllerForServiceType:
  6.                                          SLServiceTypeTwitter];
  7.  
  8.   // Sets the completion handler.  Note that we don't know which thread the
  9.   // block will be called on, so we need to ensure that any required UI
  10.   // updates occur on the main queue
  11.   tweetSheet.completionHandler = ^(SLComposeViewControllerResult result) {
  12.     switch(result) {
  13.         //  This means the user cancelled without sending the Tweet
  14.       case SLComposeViewControllerResultCancelled:
  15.         break;
  16.         //  This means the user hit 'Send'
  17.       case SLComposeViewControllerResultDone:
  18.         break;
  19.     }
  20.   };
  21.  
  22.   //  Set the initial body of the Tweet
  23.   [tweetSheet setInitialText:@"just setting up my twttr"];
  24.  
  25.   //  Adds an image to the Tweet.  For demo purposes, assume we have an
  26.   //  image named 'larry.png' that we wish to attach
  27.   if (![tweetSheet addImage:[UIImage imageNamed:@"larry.png"]]) {
  28.     NSLog(@"Unable to add the image!");
  29.   }
  30.  
  31.   //  Add an URL to the Tweet.  You can add multiple URLs.
  32.   if (![tweetSheet addURL:[NSURL URLWithString:@"http://twitter.com/"]]){
  33.     NSLog(@"Unable to add the URL!");
  34.   }
  35.  
  36.   //  Presents the Tweet Sheet to the user
  37.   [self presentViewController:tweetSheet animated:NO completion:^{
  38.     NSLog(@"Tweet sheet has been presented.");
  39.   }];
  40. }