What is the best approach for building an iphone client for a rails app?

后端 未结 6 1442
感动是毒
感动是毒 2020-12-23 18:22

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

相关标签:
6条回答
  • 2020-12-23 18:42

    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.

    0 讨论(0)
  • 2020-12-23 18:47

    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.

    0 讨论(0)
  • 2020-12-23 18:51

    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.

    0 讨论(0)
  • 2020-12-23 18:58

    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)

    0 讨论(0)
  • 2020-12-23 19:07

    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.

    0 讨论(0)
  • 2020-12-23 19:07

    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

    0 讨论(0)
提交回复
热议问题