2011-12-01 20 views
3

我想使用twitter api twitter + OAuth從我的iphone上分享圖像,但我不知道如何從我的iphone分享。我使用sharekit嘗試了相同的操作,但是我沒有獲得成功。你會告訴我如何使用twitter api在iphone上分享twitter上的圖片嗎?我怎樣才能發送圖像在我的iPhone在ios5的twitter上

+0

你只是想在twitter上分享你的圖片鏈接,或者你想先上傳圖片,然後獲取圖片鏈接,然後在twitter上上傳該鏈接。 – Gypsa

+0

我要在twitter上發佈圖片 – iPhone

回答

5

由zot提供的鏈接工作,但我不得不添加一些代碼,我必須添加兩個框架Twitter.framework和Account.framework。修改後的代碼如下

- (IBAction)sendCustomTweet:(id)sender { 
    // Create an account store object. 
    ACAccountStore *accountStore = [[ACAccountStore alloc] init]; 

    // Create an account type that ensures Twitter accounts are retrieved. 
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; 

    // Request access from the user to use their Twitter accounts. 
    [accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) { 
     if(granted) { 
      // Get the list of Twitter accounts. 
      NSArray *accountsArray = [accountStore accountsWithAccountType:accountType]; 


      if ([accountsArray count] > 0) { 
       // Grab the initial Twitter account to tweet from. 
       ACAccount *twitterAccount = [accountsArray objectAtIndex:0]; 


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


       TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"https://upload.twitter.com/1/statuses/update_with_media.json"] 
                  parameters:[NSDictionary dictionaryWithObject:@"Hello. This is a tweet." forKey:@"status"] requestMethod:TWRequestMethodPOST]; 

        // "http://api.twitter.com/1/statuses/update.json" 

       [postRequest addMultiPartData:UIImagePNGRepresentation(image) withName:@"media" type:@"multipart/png"]; 


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

       [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]; 
       }]; 
      } 
     } 
    }]; 
} 
+0

你有更新的iOS 9嗎? –

1

你檢查了這一點:iOS 5 Attach photo to Twitter with Twitter API

這隻適用於iOS5的手機。

在我的應用程序中,這是iOS5以前的版本,我使用TwitPic上傳照片併發布Twitter狀態。有一個關於如何使用ASHttpRequest在這裏做Twitter的狀態更新一個很好的教程:如果你想追求這種方法讓我知道,因爲就是你需要做的,超出了本教程的額外工作

http://www.icodeblog.com/2009/07/09/integrating-twitter-into-your-applications/

你也需要從TwitPic獲得一個API密鑰。