Cocoa in the Shell

[iOS 5] Twitter integration framework

One of the new feature that comes with iOS 5 is the native Twitter integration. You just have to register your account(s) in the Settings application and you are done, you can easily tweet your photos, links… with a nice looking interface. What’s even better for us developers, is that it’s really easy to take advantage of this feature.

No more pain to integrate Twitter within your app. No more external source code that triggers tons of warnings when you are building, especially if, like me, you enable most of the warnings flags.
Now all you need is to link with Twitter.framework, write a couple lines of code and you are done ! Of course if you intend to support older iOS version you have to weak link against the framework.

The few lines of code you need to write look like this :

    Class twClass = NSClassFromString(@"TWTweetComposeViewController");
    if (!twClass) // Framework not available, older iOS
    if ([TWTweetComposeViewController canSendTweet]) // Check if twitter is setup and reachable
        TWTweetComposeViewController* twc = [[TWTweetComposeViewController alloc] init];
        [twc addURL:[NSURL URLWithString:@""]];
        [twc addImage:[UIImage imageNamed:@"Some image.png"]]
        [twc setInitialText:@"iOS 5 Twitter tutorial"];
        [_viewController presentViewController:twc animated:YES completion:^{
            // Optional
        // Assume twc is ARC released or call [twc release];
        // Twitter account not configured, inform the user
  • You don’t need to shorten URL(s), it will be done automatically by the framework.
  • You can add several images or URLs.

The addImage:, addURL: and setInitialText: methods return booleans values to indicate if the operations were successful. The reason of failure are :

  • The Image / URL / Text doesn’t fit in the character space (> 140)
  • The view was presented to the user

And… that’s all there is to know ! Pretty neat no ?