1
votes

I'm working on a document based app with UIDocumentBrowserViewController. The app itself creates txt files which the user can edit and save.

I want to send those files to a companion app on an Apple Watch. The user can then read those on the watch. The UIDocument is read by my app and converted to be sent with WCSession.

The question is now: How can I uniquely identify an UIDocument?

I know I could make my own identifier by putting a UUID in the file name or at the end of each text file, but this is not the sweet solution.

2

2 Answers

0
votes

Apple recommends the UUID for a filename approach, but does mention that you can have the user change the display name after it is created.

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:

  • Generate a UUID (universally unique identifier) for each document, optionally with an application-specific prefix.

  • Generate a timestamp (date and time) for each document, optionally with an application-specific prefix.

  • Use a sequential numbering system, for example: “Notes 1”, “Notes 2”, and so on.

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.

0
votes

Why don't you use URLResourceKey.documentIdentifierKey ? This is a persistent identifier which tracks a document across renames and safe-saves.