Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 5 Attach photo to Twitter with Twitter API

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.

like image 429
Rotten Avatar asked Nov 14 '11 22:11

Rotten


People also ask

How do I put a picture on Twitter from my iPhone?

Tap the Tweet icon ( on iOS or Android). Tap the photo icon to take a photo or to select one from your camera roll. Tap the sticker icon from the selected photo to launch a library of stickers to choose from. Tap the sticker(s) you want and then drag with your finger to where you place it where you want it.

Why can't I post pictures on Twitter?

Supported Formats. Images uploaded to Twitter must be smaller than 3MB in size and saved as a GIF, JPEG or PNG file. The BMP, TIFF and animated GIF file formats are not supported by Twitter. Note that you can only upload one image per tweet, whether you are using the main Twitter website or a third-party application.

How do I upload media to Twitter?

Uploading mediaEnsure you're logged in and viewing studio.twitter.com. Click the Upload button at the top-right of the page. Select one or more media files from your computer. Once your media has been selected, upload will start automatically.

Does Twitter remove EXIF data?

It also says EXIF data is not available to people who see the photo attached to your tweet. While Twitter may strip out the EXIF data from any picture you send from your account, you can share your location voluntarily when posting a photo. To do so, just select the map pin icon in the box when composing a post.


3 Answers

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)

like image 182
staticfiction Avatar answered Oct 07 '22 13:10

staticfiction


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];
like image 38
aporat Avatar answered Oct 07 '22 12:10

aporat


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.

like image 30
Noah Witherspoon Avatar answered Oct 07 '22 11:10

Noah Witherspoon