Consuming OData from IPHONE

Hi,

We have seen in the previous post how to consume OData services from Windows Phone 7, this time I want to show you how to do it from an iPhone.

1. To consume OData from an iPhone App I have used a library called “OData Client for Objective-C”. It can be downloaded from here: http://odataobjc.codeplex.com/

The library contains some examples using the Netflix Catalog that can help you get familiar with more advanced stuffs than what are going to be explained in this article.

2. Once you downloaded the library the first thing you have to do on XCode is to add the references to the OData library.

This is done by accessing the Project Properties:

clip_image002

3. On the Build tab of the Properties window locate the section “Search Paths” and add the following reference in “Header Search Paths” and “Library Search Paths”:

clip_image004

Also make sure that you selected the recursive checkbox to both paths.

4. After both paths are added you need to add the library to the Frameworks folder of the solution, looking into the file system for the libMSODataLib.a located at this relative path inside the OData SDK folder: “/framework/bin/odatalib/lib/iPhoneSimulatorLibs/iPhone_Simulator_4.1/release/libMSODataLib.a”

clip_image006

5. After that’s done you will have the library for the project ready to be used.

6. Creating the proxy class.

To connect to OData you need to generate the proxy class. To do that open terminal, navigate to the folder where the odatagen executable is located and run this command:

./odatagen /uri=http://domain.com/YourService.svc /out=/Development/odataproxy

On the folder odataproxy you will find the generated proxy, now you need to import the “.h” and “.m” file into the classes folder of the project.

7. After that’s ready, use the proxy to connect to the server at any of the classes.

In this example, I assume that the proxy generated is called CustomersEntities.

With the next line you will have the proxy connected to the server:

CustomersEntities *proxy = [[CustomersEntities alloc]initWithUri:@”URL TO SERVICE” credential:nil];

And to get data simply do the following:

NSArray *customers;

customers = [proxy Customers];

With this example you will have a list of Customers loaded using OData services.

Of course, you can run much more complex queries using OData but that’s not the main focus of this article.

Here is a screen capture of my iPhone App consuming OData :)

clip_image008

Sebastián Rodríguez

No Comments