Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use TWrequest to send an image with a text to Twitter in IOS5

Tags:

twitter

ios5

I am able to send a tweet easily using TWRequest like this as per 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 it is possible to use http://api.twitter.com/1/statuses/update_with_media.json in some way to send an image with the tweet instead of going via twitpic or another service. Or is there another way to send an image along with the tweet?

Thanks

like image 339
stuart Avatar asked Nov 07 '11 09:11

stuart


1 Answers

It is possible. You'll need to use addMultiPartData:withName:type: method for adding attributes for your tweet. Status text won't be displayed until you add it as multipart 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"];
like image 169
Andrey Kuznetsov Avatar answered Feb 19 '23 01:02

Andrey Kuznetsov