2012-07-03 5 views

Trả lời

18

Sử dụng mã sau đây để retweeting.

- (void)_retweetMessage:(TwitterMessage *)message 
{ 
    NSString *retweetString = [NSString stringWithFormat:@"http://api.twitter.com/1/statuses/retweet/%@.json", message.identifier]; 
    NSURL *retweetURL = [NSURL URLWithString:retweetString]; 
    TWRequest *request = [[TWRequest alloc] initWithURL:retweetURL parameters:nil requestMethod:TWRequestMethodPOST]; 
    request.account = _usedAccount; 

    [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 
     if (responseData) 
     { 
      NSError *parseError = nil; 
      id json = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&parseError]; 

      if (!json) 
      { 
       NSLog(@"Parse Error: %@", parseError); 
      } 
      else 
      { 
       NSLog(@"%@", json); 
      } 
     } 
     else 
     { 
      NSLog(@"Request Error: %@", [error localizedDescription]); 
     } 
    }]; 
} 

Taken From Here

Good Luck.

+0

Điều này có hữu ích không? –

+1

có, cảm ơn nó hoạt động cho retweet, bây giờ tôi đang cố gắng yêu thích và trả lời – PJR

+0

tôi sẽ thách thức chấp nhận câu trả lời ur, tôi đã bỏ phiếu nó đã có, nhưng tôi đang cố gắng yêu thích và trả lời, đó là lý do tại sao tôi đang chờ đợi. :) – PJR

1

Sử dụng TWRequest lớp thay vì TWTweetComposeViewController để truy cập tweet, tweet lại, trả lời và yêu thích. Để biết thêm thông tin, hãy sử dụng các liên kết này. Link1Link2

+0

nhờ , nhưng url và thông số nào tôi phải chuyển cho việc tweet lại, trả lời và yêu thích? – PJR

+0

@PJR kiểm tra câu trả lời của tôi. –

9

bạn bè Jennis 's câu trả lời giúp tôi cho việc tìm kiếm các giải pháp và câu trả lời của tôi là:

1) Đối với Retweet và yêu thích

Đây là một phương pháp học để gửi yêu cầu, bạn phải vượt qua khác nhau URL cho retweet và Favorite.

+ (void)makeRequestsWithURL: (NSURL *)url { 
    // 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]; 

      // For the sake of brevity, we'll assume there is only one Twitter account present. 
      // You would ideally ask the user which account they want to tweet from, if there is more than one Twitter account present. 
      if ([accountsArray count] > 0) { 
       // Grab the initial Twitter account to tweet from. 
       ACAccount *twitterAccount = [accountsArray objectAtIndex:0]; 


       // Create a request, which in this example, posts a tweet to the user's timeline. 
       // This example uses version 1 of the Twitter API. 
       // This may need to be changed to whichever version is currently appropriate. 
       TWRequest *postRequest = [[TWRequest alloc] initWithURL:url parameters:nil requestMethod:TWRequestMethodPOST]; 

       // 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]]; 
        iOS5Twitter *twitter5 = [[iOS5Twitter alloc] init]; 
        [twitter5 performSelectorOnMainThread:@selector(displayText:) withObject:output waitUntilDone:NO]; 
        [twitter5 release];    }]; 
      } 
     } 

    }]; 

} 

URL cho Retweet:
https://api.twitter.com/1/statuses/retweet/id_str.json.xml
URL cho yêu thích:
https://api.twitter.com/1/favorites/create/id_str.json
https://api.twitter.com/1/favorites/destroy/id_str.json

2) Mã cho Reply

 + (void)makeRequestForReplyWithSelectedFeed:(NSString *)status inReply2:(NSString *)updateID{ 
    // 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]; 



    NSString *trimmedText = status; 
    if ([trimmedText length] > 140.0) { 
     trimmedText = [trimmedText substringToIndex:140]; 
    } 

    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0]; 
    [params setObject:trimmedText forKey:@"status"]; 
    if (updateID > 0) { 
     [params setObject:[NSString stringWithFormat:@"%@", updateID] forKey:@"in_reply_to_status_id"]; 
    } 

    // 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]; 

      // For the sake of brevity, we'll assume there is only one Twitter account present. 
      // You would ideally ask the user which account they want to tweet from, if there is more than one Twitter account present. 
      if ([accountsArray count] > 0) { 
       // Grab the initial Twitter account to tweet from. 
       ACAccount *twitterAccount = [accountsArray objectAtIndex:0]; 


       // Create a request, which in this example, posts a tweet to the user's timeline. 
       // This example uses version 1 of the Twitter API. 
       // This may need to be changed to whichever version is currently appropriate. 
        TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"] parameters:params requestMethod:TWRequestMethodPOST]; 

       // 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]]; 
        iOS5Twitter *twitter5 = [[iOS5Twitter alloc] init]; 
        [twitter5 performSelectorOnMainThread:@selector(displayText:) withObject:output waitUntilDone:NO]; 
        [twitter5 release];    }]; 
      } 
     } 
    }]; 
} 

phương pháp Call:

[iOS5Twitter makeRequestForReplyWithSelectedFeed:[NSString stringWithFormat:@"%@", tweetMessage.text ]inReply2:[ NSString stringWithFormat:@"%@",selectedFeed.id_str]];

Tôi đã tạo ra một lớp iOS5Twitter và thực hiện một phương pháp học, nếu bạn muốn làm điều đó trong điều khiển của bạn, vui lòng thay delegate bởi self

+0

Hi PJR .. Tôi nhận được 'code = 34; message = "Rất tiếc, trang đó không tồn tại"; 'trong khi chuyển url https://api.twitter.com/1/favorites/create/id_str.json. Bạn có thể giúp tôi làm điều này không? –

+0

@ShahPaneri nó có thể là một vấn đề thay đổi trong thư viện.tôi nghĩ bây giờ một ngày twitter đã thay đổi thư viện của nó để nhìn vào đó, nó có thể giúp bạn. – PJR

+0

Tôi đã tìm thấy sự cố. Tôi đã không xác thực người dùng vì vậy nó đã cho tôi lỗi. B.T.W. cảm ơn về câu trả lời. :) –