唯一标识 UIDocument

Identify UIDocument uniquely

我正在使用 UIDocumentBrowserViewController 开发基于文档的应用程序。该应用程序本身会创建用户可以编辑和保存的 txt 文件。

我想将这些文件发送到 Apple Watch 上的配套应用。然后用户可以在手表上阅读这些内容。 UIDocument 由我的应用程序读取并转换为与 WCSession 一起发送。

现在的问题是:如何唯一标识一个UIDocument?

我知道我可以通过在文件名中或每个文本文件的末尾放置一个 UUID 来创建自己的标识符,但这不是好的解决方案。

Apple recommends the UUID for a filename approach,但确实提到您可以让用户在创建后更改显示名称。

The UIDocument class assumes a correspondence between the filename of a document and the document name (also known the display name). By default, UIDocument stores the filename as the value of the localizedName property. However, an application should not require a user to provide the document filename or display name when he or she creates a new document.

For your application, you should devise some convention for automatically generating the filenames for your new documents. Some suggestions are:

  • 为每个文档生成一个UUID(通用唯一标识符), 可选地使用特定于应用程序的前缀。

  • 为每个文档生成一个时间戳(日期和时间),可选择使用特定于应用程序的前缀。

  • 使用顺序编号系统,例如:“Notes 1”、“Notes 2”等。

For the document (display) name, you might initially use the document filename if that makes sense (such as with “Notes 1”). Or, if the document contains text and the user enters some text in the document, you might use the first line (or some part of the first line) as the display name. Your application can give users some way to customize the document name after the document has been created.

你为什么不使用 URLResourceKey.documentIdentifierKey?这是一个持久标识符,可在重命名和安全保存过程中跟踪文档。