iOS Developer

[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.

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.

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!

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”);
}