Use TWrequest to send image with text to Twitter in iOS5

I can send tweets easily using TWRequest, as in the apple example,

ACAccountStore *account = [[ACAccountStore alloc] init]; ACAccountType *accountType = [accountaccountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; // Request access from the user to access their Twitter account [account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) { // Did user allow us access? if (granted == YES) { // Populate array with all available Twitter accounts NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType]; // Sanity check if ([arrayOfAccounts count] > 0) { // Keep it simple, use the first account available ACAccount *acct = [arrayOfAccounts objectAtIndex:0]; // Build a twitter request TWRequest *postRequest = [[TWRequest alloc] initWithURL: [NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"] parameters:[NSDictionary dictionaryWithObject:@"tweet goes here" forKey:@"status"] requestMethod:TWRequestMethodPOST]; // Post the request [postRequest setAccount:acct]; // Block handler to manage the response [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { NSLog(@"Twitter response, HTTP response: %i", [urlResponse statusCode]); }]; 

but I was wondering if you can somehow use http://api.twitter.com/1/statuses/update_with_media.json to send an image using a tweet instead of going through twitpic or another service. Or is there another way to send an image along with a tweet?

thanks

+8
ios5 twitter
source share
1 answer

It is possible. You will need to use the addMultiPartData: withName: type: method to add attributes for your tweet. The status text will not be displayed until you add it as multi-page data.

 TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"https://upload.twitter.com/1/statuses/update_with_media.json"] parameters:nil requestMethod:TWRequestMethodPOST]; NSData *myData = UIImagePNGRepresentation(img); [postRequest addMultiPartData:myData withName:@"media" type:@"image/png"]; myData = [[NSString stringWithFormat:@"Any status text"] dataUsingEncoding:NSUTF8StringEncoding]; [postRequest addMultiPartData:myData withName:@"status" type:@"text/plain"]; 
+14
source share

All Articles