[英]How to get Twitter account Email address using SLRequest or ACAccount?
[英]How to get the twitter timeline using SLRequest
我正在嘗試將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的任何資源。 有人可以幫忙嗎?
我使用此功能(與twitter api v1.1兼容,但需要在設備中同步twitter帳戶)在應用程序中加載tweets。我正在使用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];
}];
}
}
}];
}
}
希望它是有用的。
問題在於對api的調用是異步的。 這意味着,當您重新運行數組時,它尚未填充。 解決該問題的方法之一是satheeshwaran:下載完成后發送通知。 另一種方法是創建一個在下載后調用的委托方法。 這是一個經常遇到的模式。
Brainray提供了一種解決方案。 一個更簡單的方法是,如果您要在TableViewController中加載推文,只需在dispatch_async
調用之后調用[self.tableView reloadData]
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.