2011-11-07 88 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