2011-11-07 23 views
8

tôi có thể gửi một tweet dễ dàng sử dụng TWRequest như thế này theo ví dụ táo,Sử dụng TWrequest để gửi một hình ảnh với một văn bản lên Twitter trong 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]); 
       }]; 

nhưng tôi đã tự hỏi nếu nó có thể sử dụng http://api.twitter.com/1/statuses/update_with_media.json theo cách nào đó để gửi hình ảnh bằng tweet thay vì đi qua dịch vụ twitpic hoặc dịch vụ khác. Hay có cách nào khác để gửi hình ảnh cùng với tweet không?

Cảm ơn

+0

Vậy mã cuối cùng trông như thế nào? – Ali

Trả lời

14

Có thể. Bạn sẽ cần sử dụng phương thức addMultiPartData: withName: type: để thêm thuộc tính cho tweet của bạn. Văn bản trạng thái sẽ không được hiển thị cho đến khi bạn thêm nó làm dữ liệu nhiều phần.

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

Tuyệt vời, cảm ơn Andrey, hoạt động hiệu quả. – stuart

+0

Bạn là người giỏi nhất Andrey! – theDuncs

+0

@Andrey tôi muốn gửi văn bản duy nhất hơn URL TWRequest. – Hitarth

Các vấn đề liên quan