2011-11-07 37 views
8

我能夠輕鬆地發送鳴叫使用TWRequest這樣按照蘋果的例子,使用TWrequest與文本到Twitter發送圖像在IOS5

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以某種方式發送帶有推文的圖像,而不是通過twitpic或其他服務。或者有另一種方式發送圖片以及推文?

謝謝

+0

那麼最終的代碼是什麼樣子? – Ali

回答

14

這是可能的。 您需要使用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"]; 
+0

真棒,謝謝安德烈,工作正常。 – stuart

+0

你是最棒的安德烈! – theDuncs

+0

@Andrey我只想發送文本,而不是TWRequest URL。 – Hitarth