Используйте TWrequest для отправки изображения с текстом в Twitter в IOS5 - PullRequest
8 голосов
/ 07 ноября 2011

Я могу легко отправить твит с помощью TWRequest, как это показано на примере с яблоком,

 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]);
              }];

, но мне было интересно, можно ли каким-то образом использовать http://api.twitter.com/1/statuses/update_with_media.jsonотправьте изображение с помощью твита, а не через твитпик или другой сервис.Или есть другой способ отправить изображение вместе с твитом?

Спасибо

1 Ответ

14 голосов
/ 08 ноября 2011

Это возможно.Вам нужно использовать addMultiPartData: withName: type: метод для добавления атрибутов для вашего твита.Текст статуса не будет отображаться, пока вы не добавите его как составные данные.

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"];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...