iOS 5 Twitter framework example

Since iOS 5 includes a Twitter framework, sending tweets is awfully easy. For this to work, the device obviously needs to be running iOS 5.0+ and it’s best if the device has been logged into the Twitter service (Settings > Twitter > sign in). The user is able to give or remove permission for listed applications to you your Twitter account. No authorization is required from your project now… it’s done at the OS-level if the user wants it.

So in your project, add the Twitter.framework. Include it in your class (#import <Twitter/Twitter.h>) and when you want to send a Tweet, it’s as easy as this (I am using ARC):

TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];
[twitter setInitialText:@"Enter tweet here"];//optional
[twitter addImage:[UIImage imageNamed:@"emailIcon.png"]];
[twitter addURL:[NSURL URLWithString:[NSString stringWithString:@"http://www.apple.com"]]];

if([TWTweetComposeViewController canSendTweet]){
           [self presentViewController:twitter animated:YES completion:nil]; 
        } else {
            UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Unable to tweet"
                                                                message:@"You might be in Airplane Mode or not have service. Try again later." 
                                                               delegate:self cancelButtonTitle:@"OK" 
                                                      otherButtonTitles:nil];
            [alertView show];
            return;
}


twitter.completionHandler = ^(TWTweetComposeViewControllerResult res) {
    if (TWTweetComposeViewControllerResultDone) {
        UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Tweeted"
            message:@"You successfully tweeted"
            delegate:self cancelButtonTitle:@"OK"
            otherButtonTitles:nil];
        [alertView show];
    } else if (TWTweetComposeViewControllerResultCancelled) {
        UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Ooops..."
            message:@"Something went wrong, try again later"
            delegate:self
            cancelButtonTitle:@"OK"
            otherButtonTitles:nil];
        [alertView show];
    }
    [self dismissModalViewControllerAnimated:YES];
};

The user gets a keyboard and a Twitter “dialog” is presented. Really easy (not integrated tightly to your application), but in this way it’s more secure. They have however made it very easy to add Twitter functionality to your application. They can’t change the attached image however (if there is one).

Related Posts Plugin for WordPress, Blogger...

7 thoughts on “iOS 5 Twitter framework example

  1. jimbo

    Thanks for this great example! I made two little changes to actually check the return value in the completion handler:

    if (res == TWTweetComposeViewControllerResultDone)

    else if (res == TWTweetComposeViewControllerResultCancelled)

    Reply
  2. Mr C

    Cheers for the example. I’m seeing a memory leak in Instruments just after calling dismissModalViewControllerAnimated. Is that something I have wrong or a buggy Twitter framework?

    Reply
    1. Eric Post author

      I use automatic reference counting and I haven’t run Instruments on this implementation – so I can’t say… if you aren’t using ARC are you releasing the alert?

      Reply
  3. Rachel

    When posting same comment second time it shows two alert.
    1) First is error message alert
    2) Second alert shows You successfully tweeted created by user

    Reply

Leave a Reply

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


eight × 1 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>