【问题标题】:Retweet, reply and favorite in iOS 5 Twitter with the Accounts framework使用 Accounts 框架在 iOS 5 Twitter 中转发、回复和收藏
【发布时间】:2012-07-03 18:05:27
【问题描述】:

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

现在我想借助 Accounts 框架在 iOS 5 中转发、回复和收藏。

【问题讨论】:

    标签: iphone objective-c ios ios5 twitter


    【解决方案1】:

    朋友 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

    【讨论】:

    • 嗨 PJR.. 我在传递 api.twitter.com/1/favorites/create/id_str.json 网址时收到 code = 34; message = "Sorry, that page does not exist";。你能帮我做这件事吗?
    • @ShahPaneri 这可能是图书馆更改的问题。我想现在 twitter 已经改变了它的库,所以看看它,它可能会对你有所帮助。
    • 我发现了问题。我没有对用户进行身份验证,所以它给了我错误。顺便提一句。谢谢你的回答。 :)
    【解决方案2】:

    使用TWRequest 类而不是TWTweetComposeViewController 来访问推文、转发、回复和收藏。有关更多信息,请使用这些链接。 Link1Link2

    【讨论】:

    • 谢谢,但是我必须传递哪些网址和参数才能转发、回复和收藏?
    【解决方案3】:

    使用以下代码转推。

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

    祝你好运。

    【讨论】:

    • 是的,谢谢它可以转发,现在我正在尝试收藏和回复
    • 我会坚决接受你的回答,我已经投票了,但我正在尝试收藏和回复,这就是我等待的原因。:)
    • 实际上很容易你必须用dev.twitter.com/docs/api/1/post/favorites/create/%3Aid这个url修改现有的函数,并进行适当的小改动。浏览文档dev.twitter.com/docs/api
    • 我坚持要您将您的方法添加到我的收藏和回复答案中,以便其他人可以轻松获得帮助。
    猜你喜欢
    • 2012-05-02
    • 2011-12-23
    • 2012-04-25
    • 1970-01-01
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多