在帐户框架的iOS 5 Twitter中转推,回复和collections

我在我的应用程序中整合了iOS 5 Twitter。 我可以通过TWTweetComposeViewController与Twitter和Accounts框架的集成发送推文。

现在我想借助Accounts框架在iOS 5中转发,回复和collections。

使用以下代码进行转发。

 - (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]); } }]; } 

从这里采取

祝你好运。

朋友珍妮斯的答案帮助我find我的解决scheme,答案是:

1)对于转推和collections

这是发送请求的类方法,您必须通过不同的URL来转发和collections。

  + (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:
https://api.twitter.com/1/statuses/retweet/id_str.json.xml
collections夹的url:
https://api.twitter.com/1/favorites/create/id_str.json
https://api.twitter.com/1/favorites/destroy/id_str.json

2)回复代码

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

方法调用:

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

我创build了一个类IOS5Twitter并实现一个类的方法,如果你想在你的控制器中做,请selfreplacedelegate

使用TWRequest类而不是TWTweetComposeViewController来访问推文,转推,回复和collections。 欲了解更多信息使用这些链接。 Link1和Link2