How to Persist File Access on macOS using Swift & Scoped URL Bookmarks


Bookmark URLs gathered from an NSOpenPanel to grant an App persistent access to files or folders found outside an App's sandbox.
The following tutorial was crafted from the learnings from our upcoming app Object Capture Pro. If you wish to be updated on the progress and release of this app, please subscribe using the link below.
The process and code outlined below is split into two parts:
- I: Accessing the URL of files or a folder through an NSOpenPanel and saving the URLs bookmark to SwiftData.
- II: Accessing files or a folder through a bookmarked URL that was previously saved to SwiftData.
This methodology guarantees access to files or folders after an app has been terminated.
Additionally, in the event that a bookmark is no longer valid, the process informs you that the bookmark has gone stale.
For more information on bookmarks and persistence, consult the article linked below.
Part I: Saving Bookmarks to SwiftData

Consult the tutorials and guide found below for more information on gathering the URL of a file or folder using the NSOpenPanel, creating data that represents a bookmark that has a security scope; as well as saving the Data to SwiftData.
Part II: Using SwiftData Scoped Bookmarks to access Files or Folders

Consult the tutorials and guide found below for more information on gathering data from SwiftData, converting the data into a bookmark with security scope; as well as starting/stopping access to bookmarked urls with security scopes.
Please note that if a bookmark is "stale", then an app will require that a user executes Part I again in order for the data to be accessed.
Looking to learn more about SwiftUI, Swift, Design and Technology?
Search our blog to learn more about Swift, SwiftUI, design and technology.