If you happened to be at Cloudstock last week, and at the developer keynote (if you didn’t make it, you can check out the recordings here), you would have seen a nifty little iPad app that we put together using the latest release of the Mobile SDK for iOS (v1.1 was released at Cloudstock).

The mobile app leveraged the warehouse schema used in the Force.com workbook to demonstrate how easy it is to use the REST APIs of the platform to create custom native mobile apps. You may have also noticed that we were pulling in the Chatter feed for a particular merchandise item using the Chatter REST API.

It’s been a while since I looked at the Chatter REST API, and had not tried it via the Mobile SDK so when I had to build the keynote demo, I jumped at the chance, especially considering we have a Developer Webinar next week on March 28th to discuss the most recent updates of the Chatter REST API. (Don’t forget to register!!)

Ok, end webinar plug, now back to the code.

The Mobile SDK provides a number of wrappers to make working with the the REST API easier, but technically doesn’t provide any of these wrappers for the Chatter REST API. Thankfully, the SDK makes working with any REST API exposed from Force.com amazingly simple.

Let’s look at fetching a feed:

- (void) fetchChatterFeed

{

SFRestRequest* request = [[SFRestAPI sharedInstance] requestForResources];

request.path = [NSString stringWithFormat:@"%@%@%@%@", request.path, @"/chatter/feeds/record/", _detailItem, @"/feed-items/"];

[[SFRestAPI sharedInstance] send:request delegate:self];

}

The snippet above uses the Mobile SDK to call a REST URI. In our instance we are passing in the itemId of a record and requesting all feed-items as a response. The SDK oh-so-nicely converts the JSON response into a usable array that, in our delegate method, didLoadResponse, we can assign to a table to iterate through:

NSArray *records = [response objectForKey:@"items"];

self.newsFeed = records;

[_dataTable reloadData];

The Chatter REST API is optimized for mobile devices and, as such, returns all the data that you need in a single request. This is super handy, but it does take some time to understand the structure, and traverse it correctly (the effort to understand the results is WAY less than the effort required to fetch feeds, comments, likes etc manually yourself…. basically the Chatter API rocks for retrieving Chatter information – you’ll see a bunch of great use case examples in next week’s webinar, and some roadmap stuff too, just to give you an idea on what customers are doing with the Chatter API).  So, now we have our Chatter data in an array, let’s loop through it and, in my case, format it nicely in a table cell.

// Configure the cell to show the data.

NSDictionary *obj = [self.newsFeed objectAtIndex:indexPath.row];

//actor

NSDictionary *actor = [obj objectForKey:@"actor"];

//get profile pic

NSDictionary *photo = [actor objectForKey:@"photo"];

//we need the token to get the profile photos

NSString *token = [SFRestAPI  sharedInstance].coordinator.credentials.accessToken;

NSString *profilePicUrl = [NSString stringWithFormat:@"%@%@%@", [photo  objectForKey:@"smallPhotoUrl"], @"?oauth_token=", token];

NSURL * imageURL = [NSURL URLWithString:profilePicUrl];

NSData * imageData = [NSData dataWithContentsOfURL:imageURL];

UIImage * image = [UIImage imageWithData:imageData];

cell.imageView.image = image;

NSDictionary *body = [obj objectForKey:@"body"];

cell.textLabel.text =  [NSString stringWithFormat:@"%@: %@", [actor objectForKey:@"name"], [body objectForKey:@"text"] ];

return cell;

I like to split up the subarrays like actor, and body to make it easier to work with. I can then focus on exactly what I need to do. For example, the actor array contains a photo element that I can use, along with the oauth token – handled by the Mobile SDK – to display profile images right within my app.

And, that’s about it! You could take this code and drop into your iOS app to pull in a Chatter feed using the Mobile SDK and Chatter API. If you are interested in learning more about the Chatter API in particular, definitely register for the Chatter API webinar !

 

tagged , , , Bookmark the permalink. Trackbacks are closed, but you can post a comment.