As explained in previous post, we’re going to demonstrate how RESTframework handles REST web services. I’ll be consuming a simple Django demo web service created in previous post, so either grab the sources or read the post and build it yourself. Assuming you have this service running on localhost, port 8000, we can continue.
First, download RESTframework from GitHub. With git client, simply type in terminal:
$ git clone git@github.com:ivasic/RESTframework.git |
The quickest way to get RESTframework into your project is just including all the files from RFClasses folder. RF does not have any external dependencies so building shouldn’t be a problem.
Now, we’ll also need JSONKit to parse API response and MBProgressHUD to show some progress. Grab both from GitHub and add to your project.
Fetching objects list
We’ll have 2 view controllers in our demo app. One will be a simple object list and the other one will be, even more simple, view controller for creating a new object. First, we’re going to show how to fetch the object list. We’ll need a GET request to /objects/ URL and we’ll need to parse the JSON array we get in response and show in the table view.
RFRequest* r = [RFRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8000/"] type:RFRequestMethodGet resourcePathComponents:@"objects", nil]; [MBProgressHUD showHUDAddedTo:self.view animated:YES].labelText = NSLocalizedString(@"Loading...", @""); [RFService execRequest:r completion:^(RFResponse* response) { [MBProgressHUD hideHUDForView:self.view animated:YES]; if (response.error) { UIAlertView* aiv = [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", @"") message:response.error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease]; [aiv show]; return; } self.dataSource = [response.dataValue objectFromJSONData]; [self.tableView reloadData]; }]; |
So, what we’re doing in this code snippet is:
- Creating a GET RFRequest with appropriate URL and resource path
- Attaching MBProgressHUD to our view to show spinner progress
- Executing RFRequest and defining the block to execute after RFResponse is received
- Inside this block, we simply check for errors, parse the response, add to our tableView dataSource and reloading the table
Creating new objects
Our view controller for creating new objects is responsible for creating POST requests to /objects/ URL. Again, very simple, you just need to do this:
RFRequest* r = [RFRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8000/"] type:RFRequestMethodPost resourcePathComponents:@"objects", @"", nil]; [r addParam:self.txtName.text forKey:@"name"]; [r addParam:self.lblDate.text forKey:@"date"]; [MBProgressHUD showHUDAddedTo:self.view animated:YES].labelText = NSLocalizedString(@"Submitting...", @""); [RFService execRequest:r completion:^(RFResponse* response) { [MBProgressHUD hideHUDForView:self.view animated:YES]; if (response.error) { UIAlertView* aiv = [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", @"") message:response.error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease]; [aiv show]; return; } UIAlertView* aiv = [[[UIAlertView alloc] initWithTitle:@"Success" message:@"Success" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease]; [aiv show]; [self.navigationController popViewControllerAnimated:YES]; }]; |
Similar to our previous snippet, but this time we also add some parameters to the request – name and date strings.
This is a brief overview with sample Xcode project of how RESTframework consumes RESTful web APIs. Questions welcomed via comments as usual.
Download project: RESTframework demo Xcode project download





