iOS 6 Tweet Sheet : Use Twitter in-app

Introducing in iOS 6 is a brand new tweet sheet, which is part of the new Social Framework. With this sheet, it is easy to implement sending tweets form inside your app. In this tutorial we will choose a photo from the device library and we will tweet a message including this photo.

Update: Sep 18, 2014. The rewritten tutorial in Swift for iOS 8 and Xcode 6 is available here.

Open Xcode and create a new Single View Application. For product name, use TwitterDemo and then fill out the Organization Name, Company Identifier and Class Prefix fields with your customary values. Make sure only iPhone is selected in Devices, and that the Use Storyboards is deselected and Use Automatic Reference Counting checkboxes are selected (Unit Tests will not be necessary for this project).

To implement tweeting from your app, we need the social framework go to the project settings and add the Social.framework

Let's create the User Interface. Go to viewController.m , drag a ImageView and two buttons to the view. Select the imageView and in the attributes inspector give the view a mode of "Aspect Fit". The view should look like this.

In ViewController.m, Create the following outlet property and IBAction methods in the @interface section.

@property (nonatomic, strong) IBOutlet UIImageView *imageView;

- (IBAction)tweetButtonPressed:(id)sender;
- (IBAction)chooseImagePressed:(id)sender;

Go back to viewController.xib and make the following connections

  • "Choose Image" button -> chooseImagePressed
  • "Tweet" button -> tweetButtonPressed
  • ImageView -> imageView

In ViewController.m implement the tweetButtonPressed method

- (IBAction)tweetButtonPressed:(id)sender
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:@"Look at this nice picture!"]; [tweetSheet addImage:self.imageView.image];
[self presentViewController:tweetSheet animated:YES completion:nil];
else {
//5 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"please setup Twitter" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];

Let's take a look at this method

  1. We check if there is a twitter account available on the device.
  2. We create a SLComposeViewController object, which embeds the tweet sheet and all functionality.
  3. The initial twwet text is set, also the selected image is added to the tweet.
  4. The tweet sheet is presented.
  5. If the twitter account isn't set up, we display an alert view.

We now must now implement the ability to show the Image Picker library of the device. This is made possible with the imagePickerController object. Our view controller must conform to the UIImagePickerControllerDelegate and UINavigationControllerDelegate protocol. Change the @interface line in

@interface ViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

Create a property for the ImagePickerController

@property (nonatomic, strong) UIImagePickerController *pickerController;

Now, let's implement the chooseImagePressed method

- (IBAction)chooseImagePressed:(id)sender 
self.pickerController = [[UIImagePickerController alloc] init];
self.pickerController.delegate = self;
self.pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:self.pickerController animated:YES completion:nil];
  1. Initialize the imagepicker controller.
  2. set the delegate to the view controller and set the sourcetype to the Photo Library
  3. present the imagepicker controller.

Finally, we must implement the imagePickerController:didFinishPickingMediaWithInfo method. Here we assign the chosen image from the library to the image view.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info; 
self.imageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];

[self dismissViewControllerAnimated:YES completion:nil];

Build and Run, choose a image from the Photo Library and select the tweet button.

You can download the source code of the TwitterDemo at the ioscreator repository on github.