使用Accounts框架在iOS 5 Twitter中转发,回复和收藏

时间:2012-07-03 10:09:56

标签: iphone objective-c ios ios5 twitter

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

现在我想借助帐户框架在iOS 5中转发,回复和收藏。

3 个答案:

答案 0 :(得分:18)

使用以下代码进行转发。

- (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

祝你好运。

答案 1 :(得分:9)

朋友Jennis的回答帮助我找到解决方案,答案是:

1)对于转发和收藏

这是一个发送请求的类方法,你必须为转发和收藏传递不同的URL。

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

    }];

}

转发的网址:
https://api.twitter.com/1/statuses/retweet/id_str.json.xml
收藏的网址
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]];

我创建了一个类iOS5Twitter并实现了一个类方法,如果你想在你的控制器中执行它,请delegate替换self

答案 2 :(得分:1)

使用TWRequest课程代替TWTweetComposeViewController来访问推文,转发,回复和收藏。有关更多信息,请使用这些链接Link1Link2