如何使用SLRequest获取Twitter时间线

4
我正在尝试将Twitter时间线添加到我的iOS应用程序中。 Twitter在这里提供了官方的示例代码: https://dev.twitter.com/docs/ios/making-api-requests-slrequest
我的问题是:在第70行。
if (timelineData) {
    NSLog(@"Timeline Response: %@\n", timelineData);
}

时间轴数据已成功打印到控制台。我尝试添加


self.dict = timelineDate;

并且。
return self.dict;

在函数结尾时,它实际上返回了一个空字典。我注意到该过程是在另一个线程中完成的,所以我尝试了……

dispatch_async(dispatch_get_main_queue(), ^{
    self.dict = timelineDate;
};

但它仍然无法正常工作。 这可能很容易解决,但我真的找不到苹果或推特的任何资源。有人能帮忙吗?

3个回答

2
我使用以下函数在我的应用程序中加载推文(兼容Twitter API v1.1,但需要在设备上同步Twitter帐户)。我使用TWRequest进行操作,您也可以使用SLRequest完成相同的操作。
//include twitter.framework 
#import <Twitter/Twitter.h>

+ (void)getTweetsFortwitterID:(NSString *)twitterID

{
    if(twitterID.length >0)
    {
    NSString * finalURL = [NSString stringWithFormat:@"https://api.twitter.com/1.1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=%@&count=10", twitterID];

    TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:finalURL] parameters:nil requestMethod:TWRequestMethodGET];

    ACAccountStore *accountStore = [[ACAccountStore alloc] init] ;

    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)
             {
                 NSArray *twitterAccounts = [accountStore accountsWithAccountType:accountType];

                 if([twitterAccounts count] >0
                    )
                 {
                 ACAccount *twitterAccount = [twitterAccounts objectAtIndex:0];
                 [postRequest setAccount:twitterAccount];

                 NSLog(@"request.account:%@",postRequest.account);

                 // Perform the request created above and create a handler block to handle the response.
                 NSMutableArray *tweetsArray=[[NSMutableArray alloc]init];

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

                     // Parse the responseData, which we asked to be in JSON format for this request, into an NSDictionary using NSJSONSerialization.
                     NSArray *publicTimeline = nil;
                     NSError *jsonParsingError = nil;
                     if (responseData)
                     {
                         publicTimeline = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonParsingError];
                         NSLog(@"publicTimeline : %@", publicTimeline);
                     }

                     if ([publicTimeline isKindOfClass:[NSArray class]])
                     {

                         for (int i =0; i<[publicTimeline count]; i++)
                         {
                             NSMutableDictionary *twitterDict=[[NSMutableDictionary alloc]init];

                             if ([[publicTimeline objectAtIndex:i] objectForKey:@"text"])
                             {
                                 NSLog(@"ID: %@", [[publicTimeline objectAtIndex:i] objectForKey:@"text"]);
                                 [twitterDict setObject:[[publicTimeline objectAtIndex:i] objectForKey:@"text"] forKey:@"text"];
                             }
                             if ([[publicTimeline objectAtIndex:i] objectForKey:@"created_at"])
                             {
                                 NSLog(@"ID: %@", [[publicTimeline objectAtIndex:i] objectForKey:@"created_at"]);
                                 [twitterDict setObject:[[publicTimeline objectAtIndex:i] objectForKey:@"created_at"]
                                                 forKey:@"created_at"];
                             }

                             if ([[publicTimeline objectAtIndex:i] objectForKey:@"user"])
                             {
                                 NSLog(@"ID: %@", [[publicTimeline objectAtIndex:i] objectForKey:@"created_at"]);
                                 [twitterDict setObject:[[[publicTimeline objectAtIndex:i] objectForKey:@"user"]objectForKey:@"profile_image_url"]
                                                 forKey:@"profile_image_url"];
                             }


                             [tweetsArray addObject:twitterDict];
                             NSLog(@"tweets:%@", tweetsArray);

                         }
                     }

                     if([tweetsArray count]>0)
                         [[NSNotificationCenter defaultCenter] postNotificationName:@"tweetsLoaded" object:tweetsArray];


                 }];
                 }

             }

         }];
    }


}

希望这对你有所帮助。

顺便提一下,这个不再适用于 Twitter API 1.1,我们需要实现 oAuth 和其他东西。 - Satheesh
编辑后的版本适用于 Twitter API v1.1,但我使用与设备同步的 Twitter 帐户来对自己进行身份验证以访问 Twitter 服务。 - Satheesh

0
问题在于对API的调用是异步的。这意味着当您重新运行数组时,它尚未填充。 Satheeshwaran的答案解决此问题的一种方法是:在下载完成时发送通知。另一种方法是创建一个委托方法,在下载后调用该方法。这是一个经常遇到的模式。

0

brainray 提供了一种解决方案。更简单的方法是,如果您正在 TableViewController 中加载推文,请在 dispatch_async 调用后调用 [self.tableView reloadData]


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接