Ios 如何使用SLRequest获取twitter时间线
我正在尝试将Twitter时间线添加到我的iOS应用程序中。 Twitter在此处提供了官方示例代码:Ios 如何使用SLRequest获取twitter时间线,ios,twitter,Ios,Twitter,我正在尝试将Twitter时间线添加到我的iOS应用程序中。 Twitter在此处提供了官方示例代码: 我的问题是:在第70行 if (timelineData) { NSLog(@"Timeline Response: %@\n", timelineData); } 时间线数据已成功打印到控制台。 我试图补充 self.dict = timelineDate; 及 在函数末尾,但实际上它返回一个空字典。 我注意到这个过程是在另一个线程中完成的,所以我尝试了 dispatch_as
我的问题是:在第70行
if (timelineData) {
NSLog(@"Timeline Response: %@\n", timelineData);
}
时间线数据已成功打印到控制台。
我试图补充
self.dict = timelineDate;
及
在函数末尾,但实际上它返回一个空字典。
我注意到这个过程是在另一个线程中完成的,所以我尝试了
dispatch_async(dispatch_get_main_queue(), ^{
self.dict = timelineDate;
};
但它仍然不起作用。
这可能很容易解决,但我真的无法从苹果或Twitter上找到任何资源。有人能帮忙吗?我使用此功能在我的应用程序中加载推文(与twitter api v1.1兼容,但需要在设备中同步推文帐户)。我使用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.framework
#进口
+(void)getTweetsFortwitterID:(NSString*)twitterID
{
如果(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=%@&计数=10“,推特ID];
TWRequest*postRequest=[[TWRequest alloc]initWithURL:[NSURL URLWithString:finalURL]参数:nil requestMethod:TWRequestMethodGET];
ACAccountStore*accountStore=[[ACAccountStore alloc]init];
ACAccountType*accountType=[accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierWitter];
//请求用户访问以使用其Twitter帐户。
[accountStore requestAccessToAccountsWithType:accountType with CompletionHandler:^(已授予布尔,N错误*错误)
{
如果(授予)
{
NSArray*twitterAccounts=[accountStore accountsWithAccountType:accountType];
如果([twitterAccounts count]>0
)
{
ACAccount*twitterAccount=[twitterAccounts对象索引:0];
[postRequest setAccount:twitterAccount];
NSLog(@“请求.帐户:%@”,postRequest.帐户);
//执行上面创建的请求并创建处理程序块来处理响应。
NSMutableArray*tweetsArray=[[NSMutableArray alloc]init];
[postRequest performRequestWithHandler:^(NSData*responseData,NSHTTPURLRResponse*urlResponse,NSError*错误){
//使用NSJSONSerialization将响应数据解析到NSDictionary中,我们要求该数据为此请求的JSON格式。
NSArray*公共时间线=零;
N错误*jsonParsingError=nil;
如果(响应数据)
{
publicTimeline=[NSJSONSerialization JSONObject WithData:responseData选项:0错误:&jsonParsingError];
NSLog(@“publicTimeline:%@”,publicTimeline);
}
if([publicTimeline IsKindof类:[NSArray类]])
{
对于(int i=0;i0)
[[NSNotificationCenter defaultCenter]postNotificationName:@“tweetsLoaded”对象:tweetsArray];
}];
}
}
}];
}
}
希望它有用。问题是对api的调用是异步的。这意味着当您重新运行数组时,它还没有被填充。Satheeshwara的答案是解决这个问题的一种方法:在下载完成时发送通知。另一种方法是创建一个在downloa之后调用的委托方法D
这是一种经常出现的模式。brainray提供了一种解决方案。如果您在TableViewController中加载推文,则更简单的方法是只需调用
[self.tableView reloadData]
在您的dispatch_async
调用之后。顺便说一句,这不再适用于Twitter api 1.1,我们需要实现oAuth和其他东西。编辑后的版本适用于Twitter api v1.1,但我使用与设备同步的Twitter帐户来验证自己的Twitter服务。
//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];
}];
}
}
}];
}
}