[Tutorial] How to post Tweets with iOS5

One of the new features in the upcoming iOS5 is Twitter integration. A user only have to connect to Twitter with his account once. After that, apps have the possibility to post a tweet with the users account. Off course, the user have to agree first. This all happens in a View Controller which developers can access in the TWTweetComposeViewController class. In this tutorial I’m going to show you how to use this class and start using the Twitter integration. You will be surprised how easy this class is to implement. Check the end of this article for a sample project you can download.

I don’t explain all the small details. I’m not going to talk about what IBOutlets are and how to use them, that’s the basic knowledge. Please use Google if you don’t understand these details, they are discussed over and over again!

Step 1: Add the framework to your project

First, you need to tell Xcode you want to use the Twitter framework. To do so, you need to import this framework. Click on the top level of your project. Then click on your target. After that, choose ‘Build Phases’ and add Twitter.framework with the plus button. Check this image to see where you need to click:

Step 2: Import the header files

In every class you want to use the Twitter framework, you need to import the correct header files. You can do this with the following line:
#import "Twitter/TWTweetComposeViewController.h"

Step 3: Write the following code

After you’ve imported the framework and imported the headers, your ready to start. In this example I will be using a UIButton to act as a Tweet button. The following code is an IBAction, so it can be linked with a UIButton in your .xib or .storyboard file.

View code on Pastebin

- (IBAction)shareOnTwitter:(id)sender {
    
    TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];
    
    [twitter setInitialText:@"It's really that simple!"];
    [twitter addImage:[UIImage imageNamed:@"twitter.png"]];

    [self presentViewController:twitter animated:YES completion:nil];
       
    twitter.completionHandler = ^(TWTweetComposeViewControllerResult res) {
        
        if(res == TWTweetComposeViewControllerResultDone)
        {
            
            UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Succes!" message:@"Your Tweet was posted succesfully" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            
            [alertView show];
            
            
        }else if(res == TWTweetComposeViewControllerResultCancelled)
        {
            
            UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Your Tweet was not posted" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            
            [alertView show];
            
        }
        
        [self dismissModalViewControllerAnimated:YES];
        
    };
    
}

What does this code do? First of all, it makes an new instance of the TWTwitterViewController class. After that, the default text and image are defined. The user can change this if he’d like. After everything was set, the view controller is shown on screen.

TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];
    
    [twitter setInitialText:@"It's really that simple!"];
    [twitter addImage:[UIImage imageNamed:@"twitter.png"]];

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

In the completionhandler, you define what happens if you get a response after the user submitted his Tweet or if there was an error posting the Tweet. The error message could mean a couple of things:

  • There’s no internet connection, so you can’t connect to Twitter
  • The user wants to tweet exactly the same as he did before, so the new Tweet is not posted
  • The user clicked the ‘Cancel’ button
  • An other error
twitter.completionHandler = ^(TWTweetComposeViewControllerResult res) {
        
        if(res == TWTweetComposeViewControllerResultDone)
        {
            // The Tweet is succesfully posted
            UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Succes!" message:@"Your Tweet was posted succesfully" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            
            [alertView show];
            
            
        }else if(res == TWTweetComposeViewControllerResultCancelled)
        {
            // The Tweet was not posted. This can be because there was no internet connecting, the user cancelled or Twitter couldn't post the Tweet
            UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Your Tweet was not posted" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            
            [alertView show];
            
        }
        
        // Close the viewcontroller
        [self dismissModalViewControllerAnimated:YES];
        
    };

Step 4: Add a Tweet button

Next up, you need to add a button. If the user pushes this button, the user interface for posting a Tweet will be shown. In your .xib or your .storyboard file, place a UIButton and connect it with the IBAction ‘shareOnTwitter’.

Download sample project


I’ve made a sample project you can download. You can find it over here!

I hope you find this tutorial useful. I think it’s a great framework because you can add Twitter integration very easily in your Apps. If there is anything I’ve missed, let me know in the comments so I can edit it!

Advertisements

6 thoughts on “[Tutorial] How to post Tweets with iOS5

  1. If I want users to use this, and still let other people use the old way, can I do both? Meaning like : if (deviceiOS5) {useframework} else {useMGTwitter}? Or the fact that I add the Twitter framework will prevent me from targetting iOS < 5.0? Thanks!

    • You can check for the iOS version, because it’s only available for iOS5+, but you can also try this to check if the class is available for use:

      if(NSClassFromString(@”TWTweetComposeViewController”))
      {
      NSLog(@”TWTweetComposeViewController is available on this device”);
      }
      else
      {
      NSLog(@”TWTweetComposeViewController is not available on this device”);
      }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s