简体   繁体   English

为rails应用程序构建iphone客户端的最佳方法是什么?

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

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应用程序,并希望能够从iPhone应用程序访问基本的CRUD(创建,更新,删除)操作以及我添加的查询。 Rails provides the REST API for these operations more or less out of the box. Rails或多或少开箱即可为这些操作提供REST API。

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? 在iphone上处理REST / XML部分的最佳方法是什么,是否有任何好的Objective-C库已经做过这种事情了? The operations will also need to be associated with a particular user (ie 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 . 这对我来说是一个常见的问题,所以我一直在研究一个名为ObjectiveResource的Rails ActiveResource框架的端口。

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.0已经被App Store中的一些应用程序使用,并且可以处理Rails中内置的默认to_xml和to_json资源处理。 1.01 is a bugfix release and a drop in upgrade. 1.01是一个错误修正版本和升级版本。

We are currently working on defining the feature set for 1.1. 我们目前正在为1.1定义功能集。 We have quite a few ideas already, and are discussing more on the mailing list . 我们已经有了不少想法 ,并且正在讨论邮件列表上的更多内容

I have had some luck using plists... 我有使用plist的运气...

You will need to install the plist-3.0.0 gem (gem install plist IIRC) 你需要安装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: 然后在你的iPhone应用程序设置如下:

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. 你几乎可以自由地使用json(例如,你不必滚动自己的xml方案来格式化数据,序列化就像to_plist调用一样简单)另外,sdk带有本机代码用于处理Plist档案。

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) 但是,如果你们都准备好了一个以json / xml输出的web服务,你可能想要保留它,并使用已经提到的东西解析iphone上的东西(TouchXML,json-framework)

Your best bet is to write a simple wrapper class around NSURLConnection that sets up the specific stuff for your API. 最好的办法是围绕NSURLConnection编写一个简单的包装类,为您的API设置特定的东西。 It's not particularly complicated, and it lets you tailor your class's API directly to the REST API you're targeting. 它并不是特别复杂,它可以让您直接根据您要定位的REST API定制类的API。 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. 你的类可以有一个init方法,比如initWithMethod:delegate:params:其中委托是想要异步接收结果的对象,params是所有参数的NSDictionary。 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 . 至于解析XML,我建议你查看TouchXML

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 . 如果您不喜欢解析XML(以及谁呢?)并且可以将后端配置为吐出JSON而不是XML,我强烈建议使用json-framework等库。 It's so much easier to just read data into objects than have to parse through XML nodes. 将数据读入对象要比通过XML节点解析要容易得多。

I recently put together a TouchJSON example that uses NSURLConnection. 我最近整理了一个使用NSURLConnection的TouchJSON示例。 It's very basic but should be enough to get you started with asych loading. 这是非常基本的,但应该足以让你开始使用asych加载。

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

Be careful using NSURLConnection on the iPhone. 小心在iPhone上使用NSURLConnection。 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. 如果您正在执行大量HTTP请求,请不要指望NSURLConnection可以为您工作。 Find something else, possible some curl wrapper. 找一些其他东西,可能是一些卷曲包装。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM