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

后端 未结 6 1486
感动是毒
感动是毒 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: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)

提交回复
热议问题