Build Internet has a brand new theme, and that's only the beginning. Read the full story or hide this bar

Tweet Sheet – Creating a Popup Tweet in iOS 5

Implement the new Twitter capabilities in iOS5 to create a quick tweet in your own app.

Twitter Integration Made Easy

With the release of iOS 5 on Wednesday, a flurry of new features became available to developers. From hot new features like iCloud and iMessage to Android-like notifications, one standout new feature for developers is the addition of an entire Twitter framework, allowing developers to access the Twitter-verse in a much easier way. In prior iterations of iOS, developers needed to add in third-party libraries to authenticate Twitter accounts, post tweets, search for hashtags, and so on. With the new Twitter.framework, iOS takes the wheel from the developers’ apps for the heavy lifting.

This tutorial will let you learn the basics to implementing the new Twitter framework into your iOS app to create and post tweets. By the end, you’ll have a simple app that will let you tweet any given message, link, or image!

Overview

The Twitter.framework has a pre-built modal view controller for presenting the user with a quick tweet, named TWTweetComposeViewController. This “tweet sheet” is a quick way for developers to post text, images or links from within their app, without bouncing out to Safari or the Twitter app.

iOS 5 has a new Accounts framework as well, which allows for a neat and tidy way to store usernames, passwords, etc. Apple has taken the initiative to package the Accounts framework into the new Twitter framework. This means that if an iOS 5 user logs into Twitter in any Twitter-using applications, his credentials can be stored into the iOS Settings and are able to be used by other apps as well.

What if our user is not logged in to Twitter when they get our extravagant tweeting app? Well, that is up to us as the developer to handle. For this example, I’ll show you two different ways to handle this situation. The first is to just let iOS notify the user that they need to be logged in to tweet. The second is to just not let them tweet at all. The decision is up to you! So let’s get started.


Getting Started

Since this tutorial involves new iOS features in iOS 5, it is assumed you’re already running Xcode 4+

To begin, let’s work with a simple, single-view based app. Create a new single-view project in Xcode. Let’s call it TweetThis. For the sake of being cutting edge, let’s make sure the project uses Automatic Reference Counting (ARC).

Add Twitter.framework

To take advantage of the new Twitter framework, we need to link our binary with the Twitter library. This is accomplished by clicking on your project in the Navigator sidebar, selecting your project’s target, and going to Build Phases. This should pull up a table with a few items. Click the arrow next to Link Binary With Libraries to expand the options and click the + button to add a new library. Find the library named Twitter.framework in the popup menu and select add button. Your end product should look a bit like this:

Setup your interface

For this example app, we only need a couple UI elements. In your ViewController.xib, add in a button and a label.

The button will say something like “Tweet this!” and will be linked to the action that brings up our tweet sheet.

The label we add is for displaying an error message should the user not be logged in to Twitter. Let’s give this some text like “Not logged in to Twitter” for now. We’ll simply just hide and unhide this depending of if the user can tweet or not.Last but not least, create an IBOutlet for the button (I named mine tweetButton) and label (mine was errorLabel) and an IBAction for the tweeting named tweetButtonPressed for the event touch up inside. Now let’s hit the code.

ViewController.h

Check for availability

At this point, your ViewController.h should look something like this:

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
     IBOutlet UIButton *tweetButton;
     IBOutlet UILabel *errorLabel;
}
- (IBAction)tweetButtonPressed:(id)sender;
@end

We should add in one more variable here before moving on. Add in the interface a BOOL named _canTweet. This boolean will keep track of whether or not the ability to tweet is available. What do you mean by “the ability to tweet is available”? It’s two-fold. The TWTweetComposeViewController has a method named canSendTweet. The method returns true should the user be logged in and if the device is able to reach the Twitter service. While this doesn’t mean it’ll return false if Twitter is down (which is rare anyway), it will be very helpful should the user be in Airplane Mode, or not have service, or some similar situation.

With that boolean, our ViewController header should be complete for now. Let’s head over to ViewController.m and get some work done.

ViewController.m

First things first, let’s import the Twitter framework by importing the header for it. Add the following import line at the top of your file:

#import <Twitter/Twitter.h>

Remember earlier when I mentioned that we have a couple of options to handle if our user isn’t logged in to Twitter yet? Let’s use a define to set this. Add the following line right under your import:

#define letOSHandleLogin FALSE

Since we’re going to modify interface items based on tweeting ability, let’s check our ability to tweet by overriding the loadView method as such:

- (void)loadView
{
     [super loadView];
     //Check to see if the user is able to tweet
     /**
     * This part is somewhat optional. iOS will prompt the user to log in to Twitter if they aren't already
     * However, it's best practice to do something similar to this, like show custom alerts, etc.
     **/
     if ([TWTweetComposeViewController canSendTweet]){
          _canTweet = YES;
     }
     if (letOSHandleLogin) {
          errorLabel.hidden = YES;
     } else{
          tweetButton.hidden = !(_canTweet);      //If able to tweet, show button
          errorLabel.hidden = _canTweet;          //If able to tweet, hide error
     }
}

Great! Now at this point you should be able to run your application and depending on whether or not your simulator has a Twitter account setup already, you should see the button or the error message! But that’s just step one. Step two is the fun part: composing the actual tweet!

Composing and Displaying the Tweet Sheet

The last step to creating this awesome tweet is actual composition of the tweet sheet itself. This step is actually quite simple and takes only a few lines of code. Define your IBAction for tweetButtonClicked as such:

- (IBAction)tweetButtonPressed:(id)sender {
     //Create the tweet sheet
     TWTweetComposeViewController *tweetSheet = [[TWTweetComposeViewController alloc] init];

     //Customize the tweet sheet here
     //Add a tweet message
     [tweetSheet setInitialText:@"Just learned how to use the #iOS5 Twitter Framework on @buildinternet"];

     //Add an image
     [tweetSheet addImage:[UIImage imageNamed:@"tweetThumb.png"]];

     //Add a link
     //Don't worry, Twitter will handle turning this into a t.co link
     [tweetSheet addURL:[NSURL URLWithString:@"http://buildinternet.com/2011/10/ios-creating-your-own-tweet-sheet"]];

     //Set a blocking handler for the tweet sheet
     tweetSheet.completionHandler = ^(TWTweetComposeViewControllerResult result){
          [self dismissModalViewControllerAnimated:YES];
     };

     //Show the tweet sheet!
     [self presentModalViewController:tweetSheet animated:YES];
}

Let’s go through this line by line real quick to see what’s going on. First, we alloc-init a new tweet sheet; Objective-C basics. After that, we can customize what the actual tweet is. We can set text with the setInitialText method, add an image with addImage, and add a link with addLink (which will be converted to a t.co link for you by Twitter). Next, we have to set a blocking handler for the result. This is to ensure that we don’t mess up the rest of the app while tweeting until we dismiss the modal view (that is our tweet sheet). The last line displays our tweet sheet! Build and run your app to see the tweet in all its glory.


Wrap Up

I’ve included a sample project for you to learn from here on GitHub. This project is only one example of how to use the Twitter framework. Implement it to your pleasing! Add your own images, craft your own tweets, and bring your app to the next level of the Twitter-verse.

Wordpress.com stats not installed! Posted Monday, October 17th, 2011 / Back to Top

I this post. Tweet
SPONSOR

17 Comments 2 Mentions

  1. Adrian Author Editor

    The art of pop up tweet have never been so thoroughly explained as it is in this blog. This will definitely make the task of pop up tweeting though the latest iPhones more easy with step by step instructions. Really such an useful blog. Thanks a lot for this wonderful set of instructions!

    October 19, 2011 · Reply

  2. MrLarry Author Editor

    Awesome post mate!!

    Thinking so I don’t have to :)

    October 28, 2011 · Reply

  3. Ajeenkya Author Editor

    Is there a way to know if the user changed the text we initially assign to the tweet sheet?

    November 3, 2011 · Reply

  4. Brian Author Editor

    addURL needs to be set before the initial text. Otherwise, the url does not appear in the tweet. At least that was my experience with this. Otherwise, very helpful post!

    November 4, 2011 · Reply

  5. shyamal Author Editor

    I try it . its work. thanx lot.

    November 7, 2011 · Reply

  6. Andrew Author Editor

    Finally I’ve found someone pointing out very well how to handle Twitter and Xcode 4!
    Thanks for your great tutorial.
    Can you please suggest how to simply display the tweets in a ViewController, posted from an external Twitter account? I mean, login credentials should be embedded in the app, I’d like to display the tweets posted from a well specified user, not chosen by the iphone user.

    thanks

    Andrew

    November 18, 2011 · Reply

  7. Build July Author Editor

    Combine this with Fitext jQuery and you get an amazing tool that can work for the pc as well, I hope it will manage to work without much editing needed, I will add it to the website I`m editing for mobile use.

    November 26, 2011 · Reply

  8. siddhartha sinha Author Editor

    Thank you for this information and it is working

    November 30, 2011 · Reply

  9. JZ-system Author Editor

    Thank you so much for this info!! Great tutorial!

    December 9, 2011 · Reply

  10. Wasa Author Editor

    Great tutorial! Thanks

    December 15, 2011 · Reply

  11. davy choela Author Editor

    hello,

    I tried this and it works very well on my ipad. I also tried it on my iPod but there I got a problem. In my app I hide the status bar so the “tweet sheet” goes all the way up to the top of the screen. My problem is that I can’t click the cancel or send button anymore!

    Could it be that the notification slide down menu has something to do with it?

    January 19, 2012 · Reply

    • davy choela Author Editor

      I just figured out that it has something to do with the view hierarchy. I can still move the view below the tweet sheet…

      January 19, 2012 · Reply

  12. Papon Masud Author Editor

    Awesome post Tweet Sheet – Creating a Popup Tweet in iOS 5.

    February 8, 2012 · Reply

  13. delia Author Editor

    very special !!!`

    February 13, 2012 · Reply

  14. Pressure Digger Author Editor

    Oh sweet, I’ll try if it will work on my ipad. Thx :)

    February 29, 2012 · Reply

  15. شات صوتي Author Editor

    thank for you articles

    March 6, 2012 · Reply

  16. Corner Mount Digger Derrick Author Editor

    this was very good and detailed

    March 20, 2012 · Reply

 

Join the Conversation

Back to Top / Comment RSS

2012 Build Internet. Created by One Mighty Roar. Icons by Komodo Media. Back to Top