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.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 10/25/2024 at 11:18
Last Updated on 10/25/2024 at 14:54
<p>Two flowers that represent SwiftUI and XCode with the text "Security-Scoped URL Bookmarks" beneath them.</p>

Bookmark URLs gathered from an NSOpenPanel to grant an App persistent access to files or folders found outside an App's sandbox.

SubscribeDownload Open Source Project

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.

Subscribe

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.

Apple Developer: Accessing files from the macOS App Sandbox

Part I: Saving Bookmarks to SwiftData

<p>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.</p>

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.

How to get the URL for a folder or files in SwiftHow to create a security scoped bookmark from a URL in SwiftSwiftData Quick Start Guide

Part II: Using SwiftData Scoped Bookmarks to access Files or Folders

<p>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.</p>

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.

SwiftData Quick Start GuideHow to create a security scoped bookmark from data in SwiftHow to start and stop access to security scoped bookmarks

Looking to learn more about SwiftUI, Swift, Design and Technology?

Search our blog to learn more about Swift, SwiftUI, design and technology.

Search our Blog

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.
SubscribeContact UsVisit our BlogView our ServicesView our Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe