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

Oscar de la Hera Gomez
Two flowers that represent SwiftUI and XCode with the text "Security-Scoped URL Bookmarks" beneath them.

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

An image of a flow that describes how to save a bookmark. First you must access the files or folder through the NSOpenPanel, then convert the URL to a security scoped bookmark. Finally, save the bookmark 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

A diagram that shows how to securely use a bookmark to access files or folders and perform actions on the data. The flow goes as follows: Gather Bookmark Data from SwiftData then convert Bookmark Data into a URL with Security Scope Options. If this bookmark has gone stale, you will require a new one, if it has not then you must request that the app Starts Accessing Security Scoped Resource. You then Perform actions or functionality on the files or folder and finally, ask the app to stop accessing the security scoped resource.

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.

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details