7

Is there any way to add a photo to Twitter timeline using TWRequest or anything like so?? I'm very very lost in this case.

I can post a status update using TWRequest for iOS5 and MGTwitterEngine for previous iOS version, but I'm not able to attach a UIImage to the update.

Thanks in advance for any help provided.

Rotten
  • 742
  • 10
  • 24

3 Answers3

15

I have found and used so many great answers on this site, Thanks everyone, thought it's about time to give back :) Elaborating on Noah's answer, this is the final code that worked for me to get an image and text on a tweet using TWRequest...

            TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"https://upload.twitter.com/1/statuses/update_with_media.json"] parameters:nil requestMethod:TWRequestMethodPOST];

            UIImage * image = [UIImage imageNamed:@"myImage.png"];

            //add text
            [postRequest addMultiPartData:[@"I just found the secret level!" dataUsingEncoding:NSUTF8StringEncoding] withName:@"status" type:@"multipart/form-data"];
            //add image
            [postRequest addMultiPartData:UIImagePNGRepresentation(image) withName:@"media" type:@"multipart/form-data"];

            // Set the account used to post the tweet.
            [postRequest setAccount:twitterAccount];

            // Perform the request created above and create a handler block to handle the response.
            [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                NSString *output = [NSString stringWithFormat:@"HTTP response status: %i", [urlResponse statusCode]];
                [self performSelectorOnMainThread:@selector(displayText:) withObject:output waitUntilDone:NO];
            }];

I found using TWRequest to be best solution for my scenario as I didn't want the user to be able to edit the post before tweeting (which was the issue with using TWTweetComposeViewController)

staticfiction
  • 298
  • 2
  • 11
  • Is this code applicable in only ios5? or I can use it for lower versions – iPhone Dec 02 '11 at 05:02
  • iOS5 only, there are opensource twitter libraries out there that may help you if you need a < iOS5 solution eg. MGTwitterEngine...https://github.com/mattgemmell/MGTwitterEngine/ ...though I haven't tried them myself – staticfiction Dec 06 '11 at 02:31
  • You can do the same thing in iOS 4 with [DETweetComposeViewController](https://github.com/doubleencore/DETweetComposeViewController). – Dave Batton Dec 14 '11 at 23:40
  • I have followed same code to send msg on twitter and in my previous application it works fine too but right now I am trying to send msg using this code but it shows error msg "HTTP response status: 403" then would you suggest me what is the problem? – iPhone Jan 21 '12 at 10:50
  • 1
    @Neel the TwitterAccount code is the same as in this example... http://stackoverflow.com/questions/8085785/trying-to-follow-someone-on-twitter-using-new-ios-5-api-getting-406-return-er – staticfiction Mar 19 '12 at 04:40
4

you can use the twitter compose view controller TWTweetComposeViewController to post photos to twitter without dealing with twitter oauth and accounts. see http://developer.apple.com/library/ios/#documentation/Twitter/Reference/TWTweetSheetViewControllerClassRef/Reference/Reference.html for more details.

The main issue is it's a new feature of iOS 5, so users that didn't upgrade won't be able to use it.

TWTweetComposeViewController* tweetView = [[TWTweetComposeViewController alloc] init];
[tweetView addImage:yourImage];

TWTweetComposeViewControllerCompletionHandler 
       completionHandler =
    ^(TWTweetComposeViewControllerResult result) {
        switch (result)
        {
            case TWTweetComposeViewControllerResultCancelled:
                NSLog(@"Twitter Result: canceled");
                break;
            case TWTweetComposeViewControllerResultDone:
                NSLog(@"Twitter Result: sent");
                break;
            default:
                NSLog(@"Twitter Result: default");
                break;
        }
        [self dismissModalViewControllerAnimated:YES];
    };
[tweetView setCompletionHandler:completionHandler];
aporat
  • 5,922
  • 5
  • 32
  • 54
1

With TWRequest, you can use the -addMultipartData:withName:type: method on that with the data from, e.g., the UIImageJPEGRepresentation() method, to post a tweet via the Twitter API’s statuses/update_with_media method.

If you’re using TWTweetComposeViewController, it’s even simpler: you can attach one or more UIImage images to it with the -addImage: method.

Noah Witherspoon
  • 57,021
  • 16
  • 130
  • 131