Adding iOS5 native Twitter support to your cocos2d app

In iOS5, Apple introduced native Twitter integration via the “Tweet Sheet” (aka TWTweetComposeViewController), allowing you to easily implement Twitter in your iOS app without worrying about all the heavy lifting. If you tried implementing Twitter support in the past and found yourself lost in a sea of OAuth source files, trying to handle authentication yourself, and banging your head over what to do with various responses, you’ll know how much of a relief it is that this now “just works.”

This is great for UIKit, but what if you want to implement the same simple Twitter sharing functionality in your cocos2d game? Luckily, it’s nearly as easy!

Note: You should have the latest version of Xcode installed (as of this writing I’m using 4.3.1), at least cocos2d 0.99.5 installed and working, and you should know how to create a new project with the “Hello World” cocos2d template. If you don’t, you might want to take care of this before proceeding or check out some of our previous cocos2d tutorials.

Create a new project

Create a new cocos2d application and call it TweetMe. Run the application in the iOS Simulator to make sure it’s working. You should see this:

Add the Twitter framework

Next, we’ll need to add the Twitter framework to our project. To do this:

1. Click on the project icon in the leftmost pane (the blue icon that says “TweetMe”).
2. Under Targets select “TweetMe”
3. Tap on the “Build Phases” tab.
4. Expand “Link Binary with Libraries”
5. Click the “+”

Search for “Twitter.framework”, select it, and click “Add”. Here is a little guide (click to enlarge):

Finally, in your HelloWorldLayer.m file, add the following import statement at the top:


Create a view controller

To display the Tweet Sheet, we’ll need a view controller to contain it, and then add it as a subview to the CCDirector’s OpenGL view.

In your HelloWorldLayer.h interface file, create a UIViewController property:

#import "cocos2d.h"

// HelloWorldLayer
@interface HelloWorldLayer : CCLayer
    UIViewController *viewController;

// returns a CCScene that contains the HelloWorldLayer as the only child
+(CCScene *) scene;

@property (nonatomic, retain) UIViewController *viewController;


In the implementation file, synthesize it and release it in dealloc (if you’re running one of the latest versions of cocos2d that supports ARC, you won’t need to do this):

// ...
// HelloWorldLayer implementation
@implementation HelloWorldLayer

@synthesize viewController;
// ...
// ...
- (void) dealloc
    [viewController release];
    [super dealloc];
// ...

Create a tweet button

Next, let’s create a button the user can tap to trigger the Tweet Sheet. You can use these two images for the button’s selected/unselected state:


If you’ve checked out our previous tutorial on creating simple menus in cocos2d, this code should be pretty self-explanatory. Your init method should look like this:

// on "init" you need to initialize your instance
-(id) init
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if( (self=[super init])) {
        viewController = [[UIViewController alloc] init];

	CCMenuItemImage *tweetButton = [CCMenuItemImage
        CCMenu *menu = [CCMenu menuWithItems: tweetButton, nil];
        [self addChild: menu];        
	return self;

First we’re initializing our viewController (to be used later), Next, we create a button with normal and selected state images, and have it trigger a “tweetButtonTapped” method (that we need to create) when tapped by the user. Finally, we create a menu with the button and add it to our scene.

Tweet me!

Ok, now for the fun stuff! Here is the method we’ll trigger when the user taps the tweet button:

- (void) tweetButtonTapped
    if ([TWTweetComposeViewController canSendTweet]) // Check if twitter is setup and reachable
        TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];
        // set initial text
        [tweetViewController setInitialText:@"Greetings Twitterverse!"];
        // setup completion handler
        tweetViewController.completionHandler = ^(TWTweetComposeViewControllerResult result) {
            if(result == TWTweetComposeViewControllerResultDone) {
                // the user finished composing a tweet
            } else if(result == TWTweetComposeViewControllerResultCancelled) {
                // the user cancelled composing a tweet
            [viewController dismissViewControllerAnimated:YES completion:nil];
        // present view controller
        [[[CCDirector sharedDirector] openGLView] addSubview:viewController.view];
        [viewController presentViewController:tweetViewController animated:YES completion:nil];
        // Twitter account not configured, inform the user

If you’ve used the Tweet Sheet in a UIKit-based app before, this should all look pretty familiar and pretty similar to Apple’s documentation. The key here is that we’re adding the Tweet Sheet to CCDirector’s openGLView:

[[[CCDirector sharedDirector] openGLView] addSubview:viewController.view];

To test, you’ll first want to configure a Twitter account in Settings on your device (or the Simulator). Otherwise, when you tap “Tweet!” nothing will happen. (Note: You’ll want to handle this somehow, e.g. a message asking the user to configure a Twitter account to tweet from your app).

Build and run the app, tap “Tweet!”, and you should see see the Tweet Sheet:

For fun, try changing up the message, adding a URL, and attaching an image. And of course, follow @reigndesign and @reigngames on Twitter! 😀

Stephen Ceresia

Stephen Ceresia is Marketing Manager at ReignDesign. Stephen is from Canada and is currently based in Shanghai.


  1. Yes, it would be nice to have a similar tutorial for Facebook. Also, you imply that you can setup a Twitter account on the simulator but I don’t think this is possible (you should edit your statement). If it is possible, please explain.

Leave a Reply

Your email address will not be published. Required fields are marked *

Share this post