Thursday, January 13, 2011

Consume REST Web Service using iPhone

In this tutorial we will create an iPhone application which will consume REST Web Service.

The web service which will be consumed is developed in this post.

Our final application should looked like this.
This note is taken from a REST web service which taken its data from a database server.

First, create a new Window Based Application. Use "Notes" as the project name.

Next, create a new UIViewController subclass, check the UITableViewController subclass in the options panel but leave With XIB for user interface unchecked.
Use "NotesTableViewController" as the name.
Create several variables and a method in NotesTableViewController.h



Then, we will implement the method in NotesTableViewController.m



Create another UIViewController subclass for handling new note insertion. This time, uncheck the UITableViewController subclass but check the With XIB for user interface. Use "NotesInsertViewController" as the name

Next, create some outlet and a method in our controller. Modify "NotesInsertViewController.h"



Then, double click NotesInsertViewController.xib to open Interface Builder.

Create two UITextField and one Rounded Button.
Connect both UITextField with the appropriate field: idField and contentField
Connect Rounded Button with the insertNote action

Next, implement the method in NotesInsertViewController.m



After this we can create the tab controller to navigate between the view table and insert view
Open and edit NotesAppDelegate.h



Create a UITabBarController and assign each tab item with the appropriate controller. Open and edit NotesAppDelegate.m



Build & run our application.
Notice that in inserting a new note we hardcode the date as a string and put 8 which will be interpreted by the web service as "8 seconds after 1 January 1970"

It is a little buggy and the view might not as good. But the point of the tutorial is on consuming REST Web Service, so i decide to make a minimum design and focus on the methods for handling Web Service.

Anyways, enjoy playing with REST Web Service!

3 comments: