How to create a Tweet Sheet the correct way in iOS 5 - Byron Salau

Posted by | November 17, 2011 | objective-c | One Comment

With the many new features introduced in iOS 5 I was particularly excited about the new Twitter.framework. Apple customers can now enjoy the pleasures of having their Twitter accounts baked right into the OS granting access to the twitter-verse from every nook and cranny in the device. While developers can now integrate twitter into their apps much faster and more easily than it has ever been before.

In this tutorial I will show you how to integrate the new Twitter.framework in your project as a weak link as well as posting your first tweet with a composer sheet. Unfortunantly we are not quite done with the days of including third-party libraries to authenticate twitter accounts, post tweets etc. While the new iOS5 Twitter.framework will do all the heavy lifting for us, we SHOULD still only include it as a weak link library and fallback on third-party solutions for users still yet to upgrade to iOS 5. Who are we kidding, they are never going to upgrade.

For now, i am going to assume you already know how to integrate third-party libraries and concentrate more on the Twitter.framework. If not i recommend having a looksy here later on https://dev.twitter.com/docs/twitter-libraries

Getting started

I am also assuming since you are interested in the Twitter.framework which is new. You probably have already upgraded to xcode 4.1. Otherwise some of this isnt going to make much sense to you.

To begin you will need to add the Twitter.framework to your project. Do this by clicking on your project in the Navigator sidebar, then its target, and then going to Build Phases. You should see a list of options. Expand the “Link binary with libraries” options by clicking on the arrow next to it. If you havnt already, click the + button to add a new library. In the pop-up locate the “Twitter.framework” by typing it in the search box. It should appear under the iOS 5 folder. Select it and click the add button.

You should now see the Twitter.framework in your ‘Link Binary With Libraries’ list. To the right of it you should see the word Required with two arrows pointing up and down. Click the word ‘Required’ and change it to Optional. This will reference the library as a weak link which is the important part if you intend to build for older targets such as iOS 4.

The code

First off, include the framework in your header.

#import <Twitter/Twitter.h>;

As we referenced that framework as optional, the next hurdle is to determine if the framework is available. In this tutorial we are interested in the class “TWTweetComposeViewController” so lets check for this with the following.


-(void)displayTwitterComposerSheet
{
    //Check if our weak library (Twitter.framework) is available
    Class TWTweetClass = NSClassFromString(@"TWTweetComposeViewController");
    if (TWTweetClass != nil)
    {
        //Class exists
    }
    else
    {
        //Class dosnt exist, so will need to fallback on a third-party solution
    }
}

Using the above code, you may decide not to provide any twitter features at all to users running older operating systems. It will depend how heavily your app depends on having twitter features.

Once we have determined the framework is available, lets display a tweet composor sheet with a few configurations.


-(void)displayTwitterComposerSheet
{
    //Check if our weak library (Twitter.framework) is available
    Class TWTweetClass = NSClassFromString(@"TWTweetComposeViewController");
    if (TWTweetClass != nil)
    {
        //Create the tweet sheet
        TWTweetComposeViewController *tweetSheet = [[TWTweetComposeViewController alloc] init];

        //Set initial text (optional)
        [tweetSheet setInitialText:@"Hell yeah, Just integrated the #iOS5 twitter framework in my project (via @pixelchild)"];

        //Attach an image (optional)
        [tweetSheet addImage:[UIImage imageNamed:@"anImage.png"]];

        //Add a link (optional)
        [tweetSheet addURL:[NSURL URLWithString:@"http://www.pixelchild.com.au/"]];

        //Set our completion handler with a block
        tweetSheet.completionHandler = ^(TWTweetComposeViewControllerResult result){
            [self dismissModalViewControllerAnimated:YES];
        };

        //Present the tweet sheet
        [self presentModalViewController:tweetSheet animated:YES];

        //Release unless your one of the cool kids using ARC :)
        [tweetSheet release];
    }
    else
    {
        //TODO: Add a twitter composer fallback
    }
}

The framwork will automatically shorten your links as well as take care of any authentication. If the user hasnt signed in yet, they will get a little alert asking them to configure an account.

Hope that helps :)

  • Emilie J Cronauer

    So this will allow you to create a tweetsheet for “tweetstorms”, even if you want to add like 120 tweets?