下一步Next Steps
在此次指导中,你创建了一个复杂的iOS应用,使用iCloud保存它的文档。设计一个支持 iCloud 的应用程序,涉及很多决策,即使这样,本教程只触及表面(浅层)。当你继续了解集成 iCloud 到你的应用程序,本章提出了你可能需要的下一步的方向。In this tutorial, you created a sophisticated iOS app that used iCloud to save its documents. Designing an app to support iCloud involves many decisions, though, and this tutorial only scratches the surface. This chapter suggests some directions you might take next as you continue to learn about integrating iCloud into your apps.
处理文件版本冲突问题Handling Document Version Conflicts
应用程序存储文件到 iCloud,必须做好准备,处理不同版本之间的应用存储的文件冲突。在两个不同的设备,当发生相同的文件更改时,可能发生冲突。例如,冲突可能发生在当用户编辑两个不同的设备上的同一个文档,设备目前当前开启,飞机模式,因为它们无法将更改传输到 iCloud 服务服务器 。Apps that store documents in iCloud must be prepared to handle conflicts between different versions of that document. Conflicts can occur when changes are made to the same document on two different devices. For example, a conflict can occur when the user edits the same document on two different devices that are currently in Airplane Mode, because they are unable to transmit changes to the iCloud servers.
虽然冲突不会频繁发生,应用需要处理这些未知的冲突。检测一个基于文件的应用,你必须为 UIDocument类的状态更改通知进行注册。如果文件进入 UIDocumentStateInConflict状态,你的应用需要检索文件版本冲突和设备如何更好继续。Although conflicts do not happen too often, apps need to be prepared to handle them. To detect conflicts in a document-based app, you must register for the state change notifications of the UIDocument
class. If the document enters the UIDocumentStateInConflict
state, your app needs to retrieve the conflicting document versions and decide how best to proceed.
更多关于处理版本冲突的信息,查看 iCloud Design Guide。For more information about handling version conflicts, see iCloud Design Guide.
显示上传和下载进度给用户Displaying Upload and Download Progress to the User
当文件变得更大,当传递或接收 iCloud,你可能想到提供一些反馈给用户。NSURL类的实例化保持属性,告诉你基础文件的当前传输状态。你可以用这些值决定是否一个文件要下载到本地设备,还有是否改变已经上传到 iCloud。你也可以用这些值查看当前下载和上传操作的进度。For documents that grow to be large, you might want to provide some feedback to the user when sent to and from iCloud. Instances of the NSURL
class maintain attributes that tell you the current transfer status of the underlying file. You can use these values to determine whether a file is downloaded to the local device and whether changes have been uploaded to iCloud. You can also use these values to check the current progress of download and upload operations.
访问 iCloud 状态属性的信息,查看 NSURL Class Reference。For information about accessing the iCloud status attributes, see NSURL Class Reference.
处理 iCloud 不可用的例子Handling Cases in Which iCloud Is Unavailable
启动后接着就调用 URLForUbiquityContainerIdentifier方法 的一个优势:这个方法可以让你决定是否 iCloud 在应用生命周期的前段时间是可用的。如果方法的返回值为 nil,意味着 iCloud 容器目录不能达到,通常是因为用户的设备没有配置 使用iCloud。(发展期间,无法访问 iCloud,通常意味着在你的应用的 iCloud 权限的配置中有一个错误。)One of the advantages of calling the URLForUbiquityContainerIdentifier:
method shortly after launch is that it lets you determine whether iCloud is available early in the life of your app. A return value of nil
from the method means that the iCloud container directory could not be reached—usually because the user’s device is not configured to use iCloud. (During development, being unable to access iCloud usually means there is an error in the configuration of your app’s iCloud entitlements.)
你的应用在当iCloud不可用的情况时,应该提供一个流畅的回退位置。例如,你可能存储新的用户文档到本地沙盒,然后在第一次有机会时就将这些文件转移到 iCloud。你应该默默做这些,不要打扰到用户。Your app should provide a smooth fallback position in cases when iCloud is unavailable. For example, you might store new user documents in the local sandbox and transfer them to iCloud at the first opportunity. You should do this quietly and not bother the user.
还需要意识到用户可以在关闭文件和数据选择或者删除当前 iCloud 账号时,关闭应用的 iCloud 访问。虽然这个不应该频繁发生,还是要防御的为你的应用编程,应用在后台暂停或运行时,应用准备好这种改变。具体来说,在移至到后台、返回前台时,调用 URLForUbiquityContainerIdentifier 方法来验证在视图访问任何你应用容器目录的文件时,iCloud 任然是可用的。You should also be aware that the user can turn off your app’s access to iCloud altogether by turning off the Documents & Data option or by deleting the current iCloud account. Although this should not happen often, you should still code your apps defensively and be prepared for this kind of change while your app is suspended or running in the background. Specifically, after having moved to the background and returned to the foreground, call theURLForUbiquityContainerIdentifier:
method to verify that iCloud is still available before attempting to access any files or documents in your app’s container directory.
提高文档演示界面Improving the Document Presentation Interface
简单文本编辑器的应用查找文件并顺序的呈现到它们被发现的地方。总之,你可能想要证明一个更确定的方式来展现文档到你的应用中。例如,你可以按照字母顺序排序来展现它们,或者维护一个当前排序的记录,也可以将这些信息写入到 iCloud。The Simple Text Editor app searches for documents and presents them in the order in which they are discovered. However, you might want to provide a more deterministic way of presenting documents in your own apps. For example, you could display them in alphabetical order or maintain a record of the current order and write that information to iCloud as well.
支持动态命名的文件Supporting the Dynamic Naming of Documents
本指导为新文件名字使用一个静态名字和一个动态数字的组合,但是你自己的应用应该更有创意。这里有一些关于创建好的文件名的建议:The tutorial used a combination of a static name and a dynamic number for new document names, but your own apps should be more creative. Here are some tips for creating good document names:
-
用一个最初的只是名字,指示你的应用程序创建的内容。不要只是创建“无标题的”文件。将文件名创建的更明确。例如,一个绘画程序可能为基本文档名称使用“你的创建”或者“帆布”。Use an initial name that is indicative of the content your app creates. Do not just create “Untitled” documents. Make the document names more specific. For example, a painting program might use “My Creation” or “Canvas” for the base document name.
-
为用户提供给一个简单但是不引人注目的方式来改变文档的名称。让用户点击文档名称并在适当的地方编辑它。不要发布警告或者用一个接口把用户拉出当前的上下文。Provide a simple but unobtrusive way for the user to change document names. Let the user tap the document name and edit it in place. Do not post alerts or use an interface that pulls the user out of the current context.
-
对包含文本内容的文件,坚持使用内容(代替文件名称)来定义文件名。这种方式类似于笔记应用展示信息的方式。因为用户没有直接访问底层文件系统,展现初始文件内容很多情况下比展现文件名称要好。For documents with text content, consider using that content (instead of the filename) to identify the document. This approach is similar to the way the Notes app displays information. Because users do not have direct access to the underlying file system, displaying the initial document content is often better than displaying a file name.
支持键-值存储Supporting the Key-Value Store
如果想让你的应用共享偏好设置,并且其他非至关重要的配置数据,通过 iCloud 使用 NSUbiquitousKeyValueStore 类这样做。它在 iCloud 中提供一个简单的接口来设置 键值对数据。If you want your app to share preferences and other noncritical configuration data, use the NSUbiquitousKeyValueStore
class to do so using iCloud. This class behaves in a similar way to the NSUserDefaults
class. It provides a simple interface for setting key-value pair data in iCloud.
更多关于使用类的信息,查看 Preferences and Settings Programming Guide。For more information about using the NSUbiquitousKeyValueStore
class, see Preferences and Settings Programming Guide.
使用核心数据与iCloudUsing Core Data with iCloud
核心数据为你的应用数据结构建模提供一系列的复杂精致的工具,并且在你的应用中有效的管理它们。不像 SQLite 数据库,核心数据存储可以通过 iCloud 共享给用户的设备。核心数据通过仅传送变化到 iCloud来管理这个操作,如此以来,它们可以被合并到每个设备的本地数据库。Core Data provides a set of sophisticated tools for modeling your app’s data structures and managing them efficiently in your app. Unlike live SQLite databases, Core Data stores can be shared among a user’s devices through iCloud. Core Data manages this operation by sending only the changes to iCloud, so that they can be incorporated into local databases on each device.
更多关于使用 iCloud 核心数据的信息,查看 “Designing for Core Data in iCloud”。For more information about using Core Data with iCloud, see “Designing for Core Data in iCloud”.
学习更多关于文件协调Learning More About File Coordinators
在某种程度上,你可能需要理解更多关于文件协调员在 iCloud 中扮演的角色。虽然 UIDocument 类提供文件协调员的更多动作,其中一些动作可能需要你自己来建立一个文件协调员。例如,当在简单文本编辑(Simple Text Editor)中删除文件,你需要建立一个自己的文件协调员,并用这个来执行操作。如此以来,当你可能需要使用它们时,就可以理解文件协调员在你的应用中的角色的重要性。At some point, you might need to understand more about the role file coordinators play in iCloud. Although the UIDocument
class provides the file coordinator for many actions, some actions may require you to create a file coordinator yourself. For example, when deleting files in Simple Text Editor, you had to create a file coordinator of your own and use it to perform the operation. Therefore, it is important that you understand the role file coordinators play in your app and when you might need to use them.
更多关于何时使用文件协调员的信息,查看 File System Programming Guide。For more information about when to use file coordinators, see File System Programming Guide.