Overview
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:
- Initialize a Tweet Sheet
- Configure the initial text, images, and URLs that you would like to set (if any)
- Configure the result handler
- 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.
- (void)showTweetSheet
{
// Create an instance of the Tweet Sheet
SLComposeViewController *tweetSheet = [SLComposeViewController
composeViewControllerForServiceType:
SLServiceTypeTwitter];
// Sets the completion handler. Note that we don't know which thread the
// block will be called on, so we need to ensure that any required UI
// updates occur on the main queue
tweetSheet.completionHandler = ^(SLComposeViewControllerResult result) {
switch(result) {
// This means the user cancelled without sending the Tweet
case SLComposeViewControllerResultCancelled:
break;
// This means the user hit 'Send'
case SLComposeViewControllerResultDone:
break;
}
};
// Set the initial body of the Tweet
[tweetSheet setInitialText:@"just setting up my twttr"];
// Adds an image to the Tweet. For demo purposes, assume we have an
// image named 'larry.png' that we wish to attach
if (![tweetSheet addImage:[UIImage imageNamed:@"larry.png"]]) {
NSLog(@"Unable to add the image!");
}
// Add an URL to the Tweet. You can add multiple URLs.
if (![tweetSheet addURL:[NSURL URLWithString:@"http://twitter.com/"]]){
NSLog(@"Unable to add the URL!");
}
// Presents the Tweet Sheet to the user
[self presentViewController:tweetSheet animated:NO completion:^{
NSLog(@"Tweet sheet has been presented.");
}];
}
Recommended Reading
The following items are provided by Apple, and should be consulted first if you have any questions about using SLComposeViewController. As always, you should consider the Apple documentation the definitive source of information for iOS.