2

Using The Twitter Framework in iOS 5

-

The twitter framework apple has added is a Christmas gift to all the iOS developers as twitter integration has now become a child's play. By using the twitter framework you may now post your stuff to twitter but a user only have to connect to Twitter with his account once and he/she can do this in the setting section of the iPhone or iPod and look for the twitter app settings. After that, apps have the possibility to post a tweet with the users account.

TWTweetComposeViewController class  in the twitter framework makes every thing possible if you want to tweet in the iOS 5 but if you want the twitter integration in the other devices then in that case you have to use the old methods of tweeting and in case if your application has support from iOS 4 to iOS 5 then in that case you need to perform a check and then tweet happily ever after .
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.
1

Sending In App Tweets

-

In iOS 5, Apple introduced tighter twitter integration with Twitter.framework and Accounts.framework.

Most of the latest “social” apps out there use some kind of frameworks to send out tweets from their app. With iOS 5, you no longer need to build your code against all these frameworks. iOS 5?s built in Twitter.framework manages most of these issues automatically. In this tutorial, we will go through how to use the built-in framework to send a tweet.

Steps

Note that this tutorial uses screenshots from Xcode 4. By now, you should all be using Xcode 4 and should have uninstalled Xcode 3 completely.

Step 1: Add the Twitter.framework and Accounts.framework

Open your target on the project navigator to your target’s build phase.

Click the “Link Binary with Libraries” section (illustrated above) and expand it. Click the “+” button to add a new framework and select “Twitter.Framework” to this project.

Step 2: Weak or Strong?

If you are planning to support iOS 4 (Doh!, you ought to man), you have to weak-link this framework. Click on Twitter.Framework and change the “Required” drop down to “Optional”.

If you decided to be hippy and prefer to support only iOS 5 (like the attached source) you can ignore this step.

Step 3: The TwTweetComposeViewController

Sending a Tweet is much similar to sending a In App email or an In App SMS

You create an instance of TwTweetComposeViewController, set the tweet you want to send and present it to the user.

Before you can do that, just as in the case of In app Email or SMS, you need to check if the device is capable of sending a In-App Tweet.

There could be two cases when you will not be able to send an in app tweet. First, if the user haven’t yet updated to iOS 5. Second, if the user haven’t configured his twitter accounts in the settings page.

The first case will not happen if you are planning to restrict your app to iOS 5. But in case you followed Step 2 and are weak-linking your Twitter.framework, you have to check if the class TwTweetComposeViewController is present using the Objective-C runtime method NSClassFromString.

If it returns nil, return and error, otherwise, call the class method, canSendTweet.

    if([TWTweetComposeViewController canSendTweet]) {

        TWTweetComposeViewController *controller = [[TWTweetComposeViewController alloc] init];

}

Step 4: The Meat

Now let us add some flesh to this by setting the initial text.

    if([TWTweetComposeViewController canSendTweet]) {

        TWTweetComposeViewController *controller = [[TWTweetComposeViewController alloc] init];

<strong> [controller setInitialText:@"Test Tweet"]; </strong>

}

Optionally you can add hyperlinks or images to your tweets. Added images and hyperlinks are automatically compressed and uploaded for you.
You can do this using the methods,

        [controller addURL:[NSURL URLWithString:@"http://blog.mugunthkumar.com"]];

        [controller addImage:[UIImage imageNamed:@"Test.png"]];

Step 5: The Delegate, err completionHander

In iOS 5, not just the TwTweetComposeViewController, but a lot of similar methods use completionHandler to notify the caller of status instead of delegates. So if you use TwTweetComposeViewController, it means you have to be on a minimum of iOS 4 even if you weak-link your framework.

Contrary to the popular belief, completionHandlers are much more cleaner and easy to understand that delegates. For example, in our case, our completionHandler looks like this

        controller.completionHandler = ^(TWTweetComposeViewControllerResult result)  {

            [self dismissModalViewControllerAnimated:YES];

            switch (result) {

                case TWTweetComposeViewControllerResultCancelled:

                    break;

                case TWTweetComposeViewControllerResultDone:

                    break;

                default:

                    break;

            }

        };

You can add UIAlertViews or similar constructs when tweet sending fails here.

So, that’s it! Wasn’t it easier than the traditional methods? Remember oAuth, MGTwitterEngine, xAuth, setting up accounts, getting xAuth from Twitter, and all other blah blah stuff?
Heck it was easier than even In App Email/SMS! However all these don’t come for free.

Drawbacks

The only biggest drawback I could think of is, your app doesn’t get any attribution for the tweets sent. Twitter.com or any client shows that the tweet was sent “via iOS” instead of your app name and I don’t see a way to set this up in the Framework.
This shouldn’t be a problem since Twitter is slowly “abandoning” the via text (at least on the stream page now). So what? Let’s start using Apple’s Twitter.Framework from now on, instead of the third party frameworks.

Source Code

The source code for this tutorial is available here.
TwitterTest.zip