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

发布于 2024-09-10 06:12:58 字数 236 浏览 5 评论 0原文

我正在开发一个 iPhone 应用程序,它将从服务器访问 XML 文件(或类似的文件)。我想将这些数据转换为 iPhone 上流畅的本机 UI。以我目前的知识,我已经可以通过加载文件、解析它们、编写自定义代码来填充数据结构并将数据转换为用户界面元素来做到这一点。然而,因为我知道这是 iPhone 开发中的一个常见问题,所以我倾向于认为有一种更简单的方法可以抽象一些过程。

在不重新发明轮子的情况下编写混合应用程序的最佳和最合适的方法是什么?

I'm developing an iPhone application that will access XML files (or something similar) from a server. I want to convert that data into a slick, native UI on the iPhone. 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.

What's the best and most appropriate way to write a hybrid app without reinventing the wheel?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很酷又爱笑 2024-09-17 06:12:58

有一些现成的抽象,但大多数都集中在 XMLRPC,这往往会变得有点笨拙。

您最好的选择可能是编写一个 NSXMLParserDelegate,这很简单;然后简单地使用 -initWithContentsOfURL:。使用这种方法,加载 XML 文件并解析它们成为一个步骤;然后您就可以创建自己的数据结构。到 UI 元素的转换通过通常的抽象机制(数据源和委托)进行。

这使您摆脱了外部强加的 XML 模式的约束,但它预示着您的 XML 文件相对较轻,或者可能存在(显着的)接口滞后。明智的做法是在单独的线程上加载 XML,并在更多数据可用时重新加载界面(尽管不是太频繁),特别是当每个文件超过几个 KiB 时。

编辑:一些注意事项:总的来说,如果您正在做任何复杂的事情,那么您应该避免使用 UIWebView。它不像本机控件那么快,并且结果应用程序的外观和感觉通常只是有点偏差。

另外,在我看来,您想要的或多或少是一个 XML-file =>; UITableView 类型的应用程序,或者至少在概念上类似。这真的很容易构建;最棘手的部分是找出一种 XML 格式,其中包含您想要的信息,但不会变得臃肿。事实上,我建议你从这里开始;只是一个 XML 消费者和一个导航控制器;使用它作为起点应该可以让您检查您的结构是否正常并且文件不是太大;这给我们带来了使用 UIWebView 的另一个问题:

你无法控制缓存,特别是当你使用 JS 处理或获取文件时。这对于大多数 Web 浏览来说都很好,WebKit 通常会做正确的事情,尤其是在面对合理的 Web 服务器配置时(好吧,不是实际的配置,而是配置的实际结果:合理的标头) 。

当您使用定制的解析器和缓存系统时,您拥有更大程度的控制权,并且可以使用很多技巧来确保您下载的内容永远不会超出严格意义上的需要。

There are a few ready-made abstractions, but most of them focus on XMLRPC, which tends to get a bit clunky.

Your best bet is probably to write an NSXMLParserDelegate, which is straightforward enough; and then simply create your parser using -initWithContentsOfURL:. With this method, loading the XML files and parsing them becomes one step; and you create your data structures as you go. Conversion to UI elements happen with the usual abstraction mechanisms (dataSource and delegate).

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. 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.

Edit: A few notes: On the whole you want to avoid UIWebView if you are doing anything even remotely complex. 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. This is really easy to build; the trickiest part is figuring out an XML format that contains the information you want without getting bloated. In fact, I'd recommend you start there; just an XML consumer and a Navigation Controller; 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:

You have no control over the caching, especially if you process or get the files using 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).

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文