2011-11-07 19 views
8
elma Örneğin göre böyle TWRequest kullanarak kolayca tweet göndermek mümkün duyuyorum

,Kullanım TWrequest 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]); 
       }]; 

yılında Twitter metinli bir görüntü göndermek için ama ben mümkün olup olmadığını merak twitpic veya başka bir servis yoluyla gitmek yerine tweet ile görüntü göndermek için http://api.twitter.com/1/statuses/update_with_media.json kullanın. Ya da tweet ile birlikte bir görüntü göndermek için başka bir yolu var mı?

Teşekkür

+0

Peki son kod neye benziyor? – Ali

cevap

14

mümkündür. Tweet'iniz için özellikler eklemek için addMultiPartData: withName: type: yöntemini kullanmanız gerekir. Durum metni, siz çok parçalı veri olarak ekleyene kadar görüntülenmez.

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

Başar, teşekkürler, Andrey, bu en iyi şekilde çalışıyor. – stuart

+0

Sen en iyisisin Andrey! – theDuncs

+0

@Andrey Sadece TWRequest URL'sinden daha fazla metin göndermek istiyorum. – Hitarth