【问题标题】:How to get the twitter timeline using SLRequest如何使用 SLRequest 获取 Twitter 时间线
【发布时间】:2014-02-27 04:47:40
【问题描述】:

我正在尝试将 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;
};

但它仍然无法正常工作。 这可能很容易解决,但我真的无法从 Apple 或 Twitter 中找到任何资源。有人可以帮忙吗?

【问题讨论】:

    标签: ios twitter


    【解决方案1】:

    我使用此功能在我的应用程序中加载推文(与 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 和其他东西。
    • 编辑后的版本适用于 twitter api v1.1,但我使用与设备同步的 twitter 帐户对 twitter 服务进行身份验证。
    【解决方案2】:

    问题是对api的调用是异步的。这意味着当您重新运行数组时,它还没有被填充。 satheeshwaran 的答案是解决此问题的一种方法:下载完成后发送通知。另一种方法是创建一个在下载后调用的委托方法。 这是一种经常出现的模式。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-04-24
        • 2015-10-19
        • 1970-01-01
        • 2013-07-18
        • 1970-01-01
        • 2013-06-22
        • 1970-01-01
        相关资源
        最近更新 更多