如何在iOS 7上实现喜爱的推文功能

时间:2014-09-19 08:43:10

标签: ios objective-c twitter slrequest acaccount

我使用STTwitter在iOS 7上制作Twitter应用程序。

我想在我的应用程序中添加收藏夹的功能。
为了获得用户流我使用STTwitter,然后我想收藏推文 但是STTwitter没有这样的功能,所以我决定使用ACAccountSLRequest

我编码如下所示,引用here(在幻灯片88和89上),但返回了状态代码400。 我该如何解决?任何帮助都会有很大帮助。

- (IBAction)favButtonTouchDown:(id)sender {

NSLog(@"Now trying to favorite tweet id: %@", _tweetID);

ACAccountStore *accountStore = [[ACAccountStore alloc] init];
_account = [accountStore accountWithIdentifier: ACAccountTypeIdentifierTwitter];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

[accountStore requestAccessToAccountsWithType: accountType
                                      options: nil
                                   completion:^(BOOL granted, NSError *error) {
                                       if (granted)
                                       {
                                           NSArray *accounts = [accountStore accountsWithAccountType: accountType];
                                           if ([accounts count] > 0)
                                           {
                                               _account = accounts[0];
                                               _accountID = _account.identifier;
                                               NSLog(@"Getting account is OK. Account ID is: %@", _accountID);
                                           }
                                       } else {
                                           NSLog(@"Error while getting account.");
                                       }
                                   }];

NSString *urlString =
[NSString stringWithFormat:@"https://api.twitter.com/1.1/favorites/create.json?id=%@", _tweetID];
NSLog(@"URL will be: %@", urlString);

NSURL *url = [NSURL URLWithString: urlString];

// NSDictionary *params = @{@"trim_user" : @"1"};

SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter
                                        requestMethod:SLRequestMethodPOST
                                                  URL:url
                                           parameters:nil];

[request setAccount: _account];

UIApplication *application = [UIApplication sharedApplication];
application.networkActivityIndicatorVisible = YES;

[request performRequestWithHandler:^(NSData *responseData,
                                     NSHTTPURLResponse *urlResponse,
                                     NSError *error) {
    if (responseData) {

        NSString *httpErrorMessage = nil;

        if (urlResponse.statusCode >= 200 && urlResponse.statusCode < 300) {

            NSDictionary *postResponseData =
            [NSJSONSerialization JSONObjectWithData: responseData
                                            options: NSJSONReadingMutableContainers
                                              error: NULL];
            NSLog(@"SUCCESS! Added Favorite Tweet with ID: %@", postResponseData[@"id_str"]);

        } else {

            httpErrorMessage =
            [NSString stringWithFormat:@"The response status code is %ld",
             (long)urlResponse.statusCode];
            NSLog(@"HTTP Error: %@", httpErrorMessage);

        }

    } else {

        NSLog(@"ERROR: An error occured while posting %@", [error localizedDescription]);

    }

    dispatch_async(dispatch_get_main_queue(), ^{

        UIApplication *application = [UIApplication sharedApplication];
        application.networkActivityIndicatorVisible = NO;

    });

}];
}

感谢。

0 个答案:

没有答案