views:

1528

answers:

7

I have a fairly standard rails app and would like to be able to access the basic CRUD (Create, Update, Delete) operations as well as queries I have added, from an iPhone app. Rails provides the REST API for these operations more or less out of the box.

What is the best approach to deal with the REST/XML part on the iphone, and are there any good objective-C libraries that do this kind of thing already? The operations will also need to be associated with a particular user (i.e. authenticated).

+4  A: 

Your best bet is to write a simple wrapper class around NSURLConnection that sets up the specific stuff for your API. It's not particularly complicated, and it lets you tailor your class's API directly to the REST API you're targeting. Your class can have an init method like initWithMethod:delegate:params: where the delegate is the object that wants to receive the result asynchronously, and the params is an NSDictionary of all the parameters. You can also use a property on the class to set up authentication (or have the class discover and record that itself after you log in).

As for parsing XML, I'd recommend checking out TouchXML.

Kevin Ballard
+2  A: 

If you don't like parsing XML (and who does?) and can configure the backend to spit out JSON instead of XML, I highly recommend using a library such as json-framework. It's so much easier to just read data into objects than have to parse through XML nodes.

Marc Novakowski
+6  A: 

I have had some luck using plists...

You will need to install the plist-3.0.0 gem (gem install plist IIRC)

Then in your controller you setup something like this:

plist = {'a'  => 'b', 'c' => 'd', 'e' => {'f' => 'g', 'h' => {'i' => 'j'}}}.to_plist
render(:text => plist)

Then in your iphone app setup something like this:

NSURL *url = [NSURL URLWithString:@"http://somewhere.com"]; 
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLResponse *response;
NSError *error;
NSData *plistData;
plistData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSPropertyListFormat format;
NSString *errorStr;
id imagesToRate = [NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&errorStr];
if(!imagesToRate) {
 NSLog(errorStr);
} else {
 NSLog(@"%@", [imagesToRate objectForKey:@"e"]);
}

You pretty much get all the freedom of using json (as in, you don't have to roll your own xml scheme to format your data, and serialization is as easy as a to_plist call) plus, the sdk comes with native code for handling plists.

But, if you all ready have a web-service that outputs in json/xml you might want to keep that and just parse the stuff on the iphone using the stuff already mentioned (TouchXML, json-framework)

diclophis
sounds interesting as I'd rather not get into XML on the phone if I don't have to...is there a way to specify you want a plist result when making the request (like rails does for xml?). Or do you just provide a different api endpoint/parameter to specify that you want the results as a plist?
frankodwyer
you could work some magic with routes to tweak the url for a given action and force a specific format then use the responds_to deal in the controller. Or, as you said just use a dedicated endpoint for the plist stuff (thats what I do)
diclophis
+1  A: 

I recently put together a TouchJSON example that uses NSURLConnection. It's very basic but should be enough to get you started with asych loading.

http://github.com/twoism/touchjsonexample/tree/master

+12  A: 

This is a common problem for me, so much so that I've been working on a port of the Rails ActiveResource framework called ObjectiveResource.

We've had two releases of the project so far. 1.0 is already in use by a few applications in the App Store and can handle the default to_xml and to_json resource handling built into Rails. 1.01 is a bugfix release and a drop in upgrade.

We are currently working on defining the feature set for 1.1. We have quite a few ideas already, and are discussing more on the mailing list.

Josh Vickery
sounds interesting, I'll check that out too.
frankodwyer
The more I look at this, the more I like it. Going to try this first.
frankodwyer
+1  A: 

Be careful using NSURLConnection on the iPhone. It is known to have memory leaks which can crash an app very quickly. If you're doing lots of HTTP requests, don't expect NSURLConnection to work for you. Find something else, possible some curl wrapper.

A: 

Check out my plistifier plugin http://github.com/jeena/plistifier

Jeena