Parsing Twitter JSON Feeds in iPhone: Adding Arrays to Cell Row
Introduction
In this article, we’ll explore how to parse Twitter JSON feeds in an iPhone app using Objective-C and Swift. We’ll also discuss how to add arrays of data from the Twitter API into a table view cell row.
Understanding the Problem
The original poster is trying to fetch the list of followers for a user, extract their names and profile pictures, and display them in a table view. However, they’re encountering issues with parsing the JSON data correctly and displaying it in the table view.
Parsing Twitter Feeds
To parse Twitter feeds, we’ll use the ACAccountStore
class to request access to the Twitter account, and then fetch the followers’ IDs using the TWRequest
class. We’ll also send a follow-up request to retrieve the names and profile pictures for each user.
Requesting Access to Twitter Account
First, let’s request access to the Twitter account:
-(void) fetchData {
ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error){
// ...
Fetching Followers’ IDs
Next, we’ll fetch the followers’ IDs using the TWRequest
class:
TWRequest *fetchFriendsFollowers = [[TWRequest alloc]
initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.twitter.com/1/followers/ids.json?screen_name=%@", acct.username]]
parameters:nil
requestMethod:TWRequestMethodGET];
Processing the Response
Once we receive the response, we’ll parse it to extract the followers’ IDs:
if ([urlResponse statusCode] == 200) {
dispatch_async(dispatch_get_main_queue(), ^{
NSError *jsonParsingError = nil;
NSDictionary *response = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonParsingError];
self.responseArray = [response objectForKey:@"ids"];
// ...
Fetching Names and Profile Pictures
For each follower’s ID, we’ll send a follow-up request to retrieve their name and profile picture:
TWRequest *fetchFriendsFollowersNames = [[TWRequest alloc]
initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.twitter.com/1/users/lookup.json?user_id=%@",user_id]]
parameters:nil
requestMethod:TWRequestMethodPOST];
[fetchFriendsFollowersNames performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error){
// ...
Processing the Response
Once we receive the response, we’ll parse it to extract the name and profile picture:
if ([urlResponse statusCode] == 200) {
dispatch_async(dispatch_get_main_queue(), ^{
NSError *jsonParsingError = nil;
NSArray *response = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonParsingError];
for (NSDictionary *user in response) {
[self.userNameArray addObject:[user objectForKey:@"name"]];
// ...
Displaying the Data in a Table View
To display the data in a table view, we’ll create a custom UITableViewCell
class that takes into account the number of followers:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Display the name and profile picture
cell.textLabel.text = [self.userNameArray objectAtIndex:indexPath.row];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.userNameArray count];
}
Note that we’re using indexPath.row
to access the name from the array, which takes into account the number of followers.
Conclusion
In this article, we explored how to parse Twitter JSON feeds in an iPhone app using Objective-C and Swift. We also discussed how to add arrays of data from the Twitter API into a table view cell row. By following these steps, you should be able to create a working example that displays the names and profile pictures for each follower.
Additional Tips and Variations
- To improve performance, consider caching the parsed data in a separate array.
- To handle errors more elegantly, consider using
NSHTTPURLResponse
to extract error codes and messages from the response. - To display additional information, such as profile pictures or tweets, consider adding additional properties to the
NSDictionary
objects extracted from the JSON response.
Last modified on 2023-06-17