2011-08-20 62 views
0

我一直在尋找Objective-C版本this PHP code sample關於如何使用Twitter的upload_with_media爲iPhone應用程序。在iOS SDK中使用Twitter API的upload_with_media?

我已經能夠找到一個。你能指點我一個例子還是解釋我如何將這段代碼翻譯成Objective-C?

感謝

+0

你試過mgtwitterengine:http://mattgemmell.com/2008/02/22/mgtwitterengine-twitter-from-cocoa – user281300

+0

是的,但不支持'upload_with_media'這是一種新的API方法。 – Christoffer

+0

我不知道你是否仍然堅持這一點,但看看[這個](http://stackoverflow.com/questions/7316776/twitters-statuses-update-with-media-on-ios-returns -500-誤差)。 –

回答

0

你可以做這樣的upload_with_media

TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"https://upload.twitter.com/1/statuses/update_with_media.json"] parameters:nil requestMethod:TWRequestMethodPOST]; 

      UIImage * image = [UIImage imageNamed:@"myImage.png"]; 

      //add text 
      [postRequest addMultiPartData:[@"I just found the secret level!" dataUsingEncoding:NSUTF8StringEncoding] withName:@"status" type:@"multipart/form-data"]; 
      //add image 
      [postRequest addMultiPartData:UIImagePNGRepresentation(image) withName:@"media" type:@"multipart/form-data"]; 

      // Set the account used to post the tweet. 
      [postRequest setAccount:twitterAccount]; 

      // Perform the request created above and create a handler block to handle the response. 
      [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 
       NSString *output = [NSString stringWithFormat:@"HTTP response status: %i", [urlResponse statusCode]]; 
       [self performSelectorOnMainThread:@selector(displayText:) withObject:output waitUntilDone:NO]; 
      }];