Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Posting image to twitter using twitterkit

I am trying to post an image and tweet using Twitters new TwitterKit with a custom UI. The only documentation they provide is how to do it with their views.

so I can figure out how to do it without an image

NSMutableDictionary *message = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[params objectForKey:@"description"],@"status",@"true",@"wrap_links", nil];

NSURLRequest* request = [twAPIClient URLRequestWithMethod:@"POST" URL:@"https://api.twitter.com/1.1/statuses/update.json" parameters:message error:nil];

[twAPIClient sendTwitterRequest:request completion:^(NSURLResponse* response, NSData* data, NSError* connectionError){



}];

But their URLRequestWithMethod method isnt mutable. how would I add an image to it. You used to do it with the SLRequest with

[postRequest addMultipartData:UIImageJPEGRepresentation(image, 0.5) withName:@"media" type:@"image/jpeg" filename:@"image.png"];
like image 785
Nick Red Avatar asked Feb 23 '15 12:02

Nick Red


People also ask

How do you post a pic on Twitter?

Tap the camera icon to add a new photo or video to your Tweet. Tap the photo icon to attach an existing photo, video, or GIF. Once you have 2 or more photos selected, you can tap and hold a photo to drag and reorder before Tweeting.

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 you post on Twitter mobile?

To post a Tweet:Tap on the Tweet icon on the top right of the screen. This will bring up the compose new Tweet box. Enter your message and then tap Tweet. The message will be posted to your Twitter profile and in the Home timeline of your followers.


1 Answers

I Have figured it out.

First you need to post the image to twitter.

NSString *media = @"https://upload.twitter.com/1.1/media/upload.json";

NSData *imageData = UIImageJPEGRepresentation(image, 0.9);

NSString *imageString = [corgiData base64EncodedStringWithOptions:0];               

NSURLRequest *request = [client URLRequestWithMethod:@"POST" URL:media parameters:@{@"media":imageString} error:&requestError];

[[[Twitter sharedInstance] APIClient] sendTwitterRequest:request completion:^(NSURLResponse *urlResponse, NSData *data, NSError *connectionError) {


}];

then in the response object you use the media_id_string and add that to the parameter of the code in my question.

So

NSMutableDictionary *message = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[params objectForKey:@"description"],@"status",@"true",@"wrap_links",mediaIDString, @"media_ids", nil];

NSURLRequest* request = [twAPIClient URLRequestWithMethod:@"POST" URL:@"https://api.twitter.com/1.1/statuses/update.json" parameters:message error:nil];

[twAPIClient sendTwitterRequest:request completion:^(NSURLResponse* response, NSData* data, NSError* connectionError){

 NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&parsingError];

}];

note the media_ids object that is from the response of the first request

NSMutableDictionary *message = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[params objectForKey:@"description"],@"status",@"true",@"wrap_links",[responseDict objectForKey:@"media_id_string"], @"media_ids", nil];

So you can just put that inside the completion block and it will post the image and tweet.

like image 90
Nick Red Avatar answered Nov 14 '22 22:11

Nick Red