Swiftpack.co -  regexident/Sandbox as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
A simple to use yet versatile API for dealing with sandboxed file access.
.package(url: "https://github.com/regexident/Sandbox.git", from: "1.1.0")


Sandbox aims to hide the awkward logistics required to let your app access files outside its own scope behind a simple and sane closure-based synchronous API, while keeping you in full control over your app's logical flow.


Sandbox provides functions for…

  • Checking whether a given resource is currently accessible from your app:
func needsPermissionForFileAtURL(fileURL: NSURL, error: NSErrorPointer = nil) -> Bool
  • Asking the user for a security scope for a given resource:
func askUserForSecurityScopeForFileAtURL(fileURL: NSURL, error: NSErrorPointer = nil) -> NSURL?
  • Handling the error-prone access-management for a security scoped resource:
func accessSecurityScopedFileAtURL(fileURL: NSURL, closure: () -> ()) -> Bool
  • And last but not least a hassle-free all-in-one function for when your "just want to access that effin' file":
func accessAndIfNeededAskUserForSecurityScopeForFileAtURL(fileURL: NSURL, closure: () -> ()) -> Bool

Example usage

Single-file access

let permissionManager = PermissionManager.defaultManager
permissionManager.accessAndIfNeededAskUserForSecurityScopeForFileURL(fileURL) {
    // read/write fileURL

Multi-file access

At some point your app may have to access dozens if not hundreds of files from potentially different directories (or even disk volumes). Accessing one file after another (and by that potentially having to ask the user for their permission) is clumsy and will annoy the user rather quickly.

Sandbox provides a convenience function for this very scenario:

func NSURL.groupByCommonPrefixURLs_sbx([NSURL]) -> ([NSURL: [NSURL]])`

It figures out the optimal grouping of resources based on their file paths and as such allows you to reduce number of permission dialogs to the very minimum, like so:

let permissionManager = PermissionManager.defaultManager
	for (groupCommonFileURL, groupFileURLs) in NSURL.groupByCommonPrefixURLs_sbx(fileURLs) {
	permissionManager.accessAndIfNeededAskUserForSecurityScopeForFileURL(groupCommonFileURL) {
		for fileURL in groupFileURLs {
			// read/write fileURL


Sandbox contains a demo app. (Note: Make sure to either type in or copy&paste file paths into its text field as with drag&drop the operating system will automatically create a security scope for you.)


Just copy the files in "Sandbox/Classes/..." into your project.

Alternatively you can install Sandbox into your project with Carthage (github 'regexident/Sandbox') or with CocoaPods (pod 'Sandbox')


Sandbox is implemented in 100% Swift.




OS X 10.9+


Vincent Esche (@regexident)


Sandbox is available under a modified BSD-3 clause license with the additional requirement of attribution. See the LICENSE file for more info.


Stars: 25
Last commit: 1 year ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API