Why does macOS not assign unique identifiers to files?
Bookmark remains persistent across reboots. "File reference URL" (file:///.file/id=367.27/
) is not reboot safe.
If user relocates a file, both "String-based path" (/Users/me/a.txt
) and "Path-based URL" (file://localhost/Users/me/a.txt
) break too.
Locating Files Using Bookmarks
If you want to save the location of a file persistently, use the bookmark capabilities of NSURL. A bookmark is an opaque data structure, enclosed in an NSData object, that describes the location of a file. Whereas path- and file reference URLs are potentially fragile between launches of your app, a bookmark can usually be used to re-create a URL to a file even in cases where the file was moved or renamed.
To create a bookmark for an existing URL, use the
bookmarkDataWithOptions:includingResourceValuesForKeys:relativeToURL:error:
method ofNSURL
. Specifying theNSURLBookmarkCreationSuitableForBookmarkFile
option creates anNSData
object suitable for saving to disk. Listing 2-3 shows a simple example implementation that uses this method to create a bookmark data object.
source