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).
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.
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With