简体   繁体   English

编写混合iPhone应用程序的最佳方法是什么?

[英]What is the best way to write a hybrid iPhone app?

I'm developing an iPhone application that will access XML files (or something similar) from a server. 我正在开发一个iPhone应用程序,它将从服务器访问XML文件(或类似内容)。 I want to convert that data into a slick, native UI on the iPhone. 我想将这些数据转换为iPhone上的漂亮本机UI。 With my current knowledge I could already do this by loading the files, parsing them, writing custom code to fill in data structures and convert the data into user interface elements. 以我目前的知识,我已经可以通过加载文件,解析它们,编写自定义代码来填充数据结构并将数据转换为用户界面元素来做到这一点。 However, since I know this is a common problem in iPhone development, I'm inclined to think that there is a simpler method that could abstract some of the process. 但是,由于我知道这是iPhone开发中的常见问题,因此我倾向于认为有一种更简单的方法可以抽象出某些过程。

What's the best and most appropriate way to write a hybrid app without reinventing the wheel? 在不重新发明轮子的情况下编写混合应用程序的最佳和最合适的方法是什么?

There are a few ready-made abstractions, but most of them focus on XMLRPC, which tends to get a bit clunky. 有一些现成的抽象,但是其中大多数集中在XMLRPC上,这往往有些笨拙。

Your best bet is probably to write an NSXMLParserDelegate , which is straightforward enough; 最好的选择是编写一个NSXMLParserDelegate ,这很简单。 and then simply create your parser using -initWithContentsOfURL: . 然后只需使用-initWithContentsOfURL:创建您的解析器。 With this method, loading the XML files and parsing them becomes one step; 使用这种方法,加载XML文件并解析它们成为一个步骤。 and you create your data structures as you go. 然后您就可以创建数据结构。 Conversion to UI elements happen with the usual abstraction mechanisms (dataSource and delegate). 到UI元素的转换是通过通常的抽象机制(数据源和委托)进行的。

This frees you from the constraint of externally imposed XML Schema, but it predicates that your XML files are relatively lightweight, or there might be (significant) interface lag. 这使您摆脱了外部强加的XML模式的约束,但可以断定您的XML文件相对较轻,或者可能存在(大量)接口滞后。 It can be sensible to load in the XML on a separate thread and reload your interface as more data becomes available (tho' not too often), especially if the files are more than a couple of KiB each. 明智的做法是将XML加载到单独的线程上,然后在有更多数据可用时( 不太经常)重新加载接口(尤其是如果每个文件都包含多个KiB时)。

Edit: A few notes: On the whole you want to avoid UIWebView if you are doing anything even remotely complex. 编辑:一些注意事项:总的来说,如果您要做的事情甚至很复杂,都希望避免使用UIWebView。 It's not as fast as native controls, and the look and feel of result applications is usually just a little off. 它不像本机控件那样快,结果应用程序的外观通常也差一点。

Also, it sounds to me like what you want is more or less an XML-file => UITableView type application, or at least something conceptually similar. 另外,在我看来,您想要的是或多或少是XML-file => UITableView类型的应用程序,或者至少是概念上相似的东西。 This is really easy to build; 真的很容易构建; the trickiest part is figuring out an XML format that contains the information you want without getting bloated. 最棘手的部分是找出一种XML格式,其中包含您想要的信息而不会without肿。 In fact, I'd recommend you start there; 实际上,我建议您从这里开始。 just an XML consumer and a Navigation Controller; 只是一个XML使用者和一个导航控制器; using that as a starting point should let you check that your structure is sane and that the files aren't too big; 以它为起点,应该让您检查结构是否合理,文件是否太大。 which brings us to another problem with using UIWebView: 使用UIWebView带来了另一个问题:

You have no control over the caching, especially if you process or get the files using JS. 您无法控制缓存,尤其是当您使用JS处理或获取文件时。 This is fine for most web browsing, where WebKit usually does the right thing, especially when faced with sane web server configurations (well, not the actual configs, but the practical results of the configuration: sane headers). 对于大多数Web浏览而言,这很好,WebKit 通常会做正确的事情,尤其是在面对合理的Web服务器配置时(不是实际的配置,而是实际的配置结果:合理的标头)。

When you use a custom-built parser and caching system, you have a greater degree of control, and a lot of tricks you can use to ensure that you never download more than you strictly speaking need. 当您使用定制的解析器和缓存系统时,您将获得更大程度的控制,并且可以使用许多技巧来确保下载的文件绝不超出严格意义上的需求。

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

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