How to get twitter timeline using SLRequest

I am trying to add a Twitter timeline to an iOS app. Twitter gives the official sample code here: https://dev.twitter.com/docs/ios/making-api-requests-slrequest
My problem: on line 70

if (timelineData) { NSLog(@"Timeline Response: %@\n", timelineData); } 

Timeline data is successfully printed on the console. I tried to add

 self.dict = timelineDate; 

and

 return self.dict; 

at the end of the function, but actually it returns an empty dictionary. I noticed that the process is running in a different thread, so I tried

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

but it still does not work. It can be very easy to solve, but I really can’t find any resources from either Apple or Twitter. Can anyone help?

+4
source share
3 answers

I download tweets in my application using this function (compatible with twitter api v1.1, but for this you need to synchronize the account for synchronization). I do this with TWRequest, you can do the same with 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]; }]; } } }]; } } 

Hope this is helpful.

+2
source

The problem is that the api call is asynchronous. This means that by the time the array is restarted, it is not yet full. Satheeshwaran's answer is one way around this: you send a notification when the download is complete. Another way is to create a delegate method that is called after loading. This is a pattern that is quite common.

0
source

brainray provides one solution. It would be easier if you loaded tweets inside the TableViewController, just call [self.tableView reloadData] after your dispatch_async call.

0
source

All Articles