// How to post to twitter in iOS 5 or greater – Guilmo

How To Post to Twitter

Published on February 17th, 2013 | by Guilherme Mogames


How to post to twitter in iOS 5 or greater

Posting to Twitter is one of the chosen social sharing by app developers, either to help promote the app by sharing it’s link, by allowing users to share content from inside the app or even for game developers to post high scores to Twitter.

Before the new iOS 5, all Twitter integrations needed to be done manually using Twitter API but when the new iOS 5 was released, native Twitter integration was added, making developers life easier to work with social sharing. In this post I’ll show you how a user can post a status to twitter really easy.

1. First requirement is that you are targeting iOS 5 or greater.

2. The second thing is to add Twitter framework to your project, and to do that you need to:

Add Library

In your project summary, scroll down to Linked Frameworks and Libraries and click in the + button.

Now scroll the list or type “twitter” to find the twitter framework, select Twitter.framwork and click on Add:

Add Twitter Library

Next step is to add the code in your .m file. For that I assume you already have a button somewhere that will execute the action to post to twitter. If you do not have a UIButton, check out my other post How to create a UIButton Programmatically.

In your .m file, first import the twitter framework by adding this to the header:

#import <Twitter/Twitter.h>

Next, inside the function that is called when the button is tapped, add this:

- (void)shareToTwitter:(id)sender{
  // Set the Twitter Class
  Class tweeterClass = NSClassFromString(@"TWTweetComposeViewController");

    if(tweeterClass != nil) {  // check for Twitter integration        
      // check Twitter accessibility and at least one account is setup
      if([TWTweetComposeViewController canSendTweet]) {
        TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init]; // Init the Twitter Controller
        [tweetViewController setInitialText:@"I highly recommend checking out Guilmo.com website for the latest iOS development tips!"];
        [tweetViewController addURL:[NSURL URLWithString:@"http://www.guilmo.com"]];

        tweetViewController.completionHandler = ^(TWTweetComposeViewControllerResult result) {
          if(result == TWTweetComposeViewControllerResultDone) {
            // the user finished composing a tweet
            NSLog(@"Tweet Done!")
          } else if(result == TWTweetComposeViewControllerResultCancelled) {
            // the user cancelled composing a tweet
            NSLog(@"User cancelled Tweet")
          // Hides the tweet controller
          [self dismissViewControllerAnimated:YES completion:nil]; 
        // Shows the tweet box
        [self presentViewController:tweetViewController animated:YES completion:nil]; 
      } else {
        // Twitter is not accessible or the user has not setup an account in the Settings Apps
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"No twitter account setup or error connecting to twitter" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [ alert show ];
    } else {
      // no Twitter integration; default to third-party Twitter framework

Basically what we did in there was, instantiate the Twitter class and checked to see if  the integration is available. Then we initiated a Tweet Composer ViewController and defined the default tweet message and the URL we are going to share.

Important: Once we add the URL through the tweetViewController addURL: method, we do not need to add the URL in the message being shared, as this is going to automatically add the URL in the end of the message.

So this is what the user will see when the button is tapped

Tweet Composer

And this is what was posted to Twitter

Screen Shot 2013-02-17 at 3.12.26 PM

You can get the example code by clicking in the link below. Just unzip and run.

Do you have any questions, comments, compliments? hit the comments below.



Tags: , , , , , ,

About the Author

Founder of the Guilmo.com website and freelance iOS developer for over 2 years. Creator of one of the most used financial app in Brazil and also creator of the iPhone game FlyingMoo. Over 10 years of experience with IT and over 4 years as a Scrum Master for a large multinational IT company.

Leave a Reply

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

Back to Top ↑