RESTful Clients in iPhone

If you are an iPhone developer, you will be disappointed by the iOS because it does not support SOAP protocol. That is, you cannot use any OOTB frameworks to consume Web Services through SOAP.


RESTful Web Services

Apple does not recommend SOAP anyway, as it is a heavyweight protocol. Instead, Apple recommends using REST style Web Services, or RESTful Web Services.


We have discussed what REST & RESTful Web Services are and how to create RESTful Web Services in Spring 3 that can dynamically generate different representations of a resource. It should be the best choice for iPhone developers to build a new project that requires communicating to the server.


The next question would be which representation format, as Spring 3 can generate XML and JSON.



XML can be governed by a schema, so that at runtime you can use it to verify if a XML document is valid. The iOS provides the NSXMLParser. It is a SAX parser, meaning that you have to write code to handle different events when the parser is parsing the XML document. It is not an easy task. You can look for other 3rd party XML libraries or parsers.



JSON stands for JavaScript Object Notation. It is a lightweight and flexible data interchange format. Again iOS does not provide any support to this format. Personally, I would go for JSON for its simplicity and flexibility.


Using the JSON Framework

I have built a very simple iPhone application that uses the JSON Framework, which is developed by Stig Brautaset. It adds a new method to the NSString class, so that you can simply invoke this method to convert a JSON string into an NSDictionary, which may contain another NSDictionary or NSArray depending on the data structure and how deep the structure is.


For example, in our previous server example, accessing this URL (http://localhost:8080/restful/app/customers.json) will produce the following JSON string.

{ "allCustomers": [
{ "name": "Customer One", "id": 1},
{ "name": "Customer Two", "id": 2}


Assuming the jsonString contains the JSON string, then the following snippet will walk through the array and extract the name and id fields of each customer.

NSDictionary *jsonResult = [jsonString JSONValue];
NSArray *customers = [jsonResult objectForKey:@"allCustomers"];
for (NSDictionary *customer in customers) {
NSString *customerName = [customer objectForKey:@"name"];
NSNumber *customerId = [customer objectForKey:@"id"];


The iPhone app uses a table view to display the details of each customer, as shown.


If you are interested in my sample program, you can download the file.