Post

Accessing App Directories with FileManager Extensions in Swift

Learn how to simplify access to sandboxed app directories in iOS using a clean FileManager extension and understand user access restrictions.

Accessing App Directories with FileManager Extensions in Swift

Accessing key directories in your app sandbox—like Documents, Caches, Library, and shared containers—can be verbose and error-prone. This extension to FileManager makes it safer and easier:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import Foundation

public extension FileManager {
    var cachesDirectory: URL {
        guard let result = try? url(for: .cachesDirectory, in: .userDomainMask, appropriateFor: nil, create: true) else {
            fatalError("Could not create and return the Caches directory.")
        }
        return result
    }

    var documentsDirectory: URL {
        guard let result = try? url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true) else {
            fatalError("Could not create and return the Documents directory.")
        }
        return result
    }

    var libraryDirectory: URL {
        guard let result = try? url(for: .libraryDirectory, in: .userDomainMask, appropriateFor: nil, create: true) else {
            fatalError("Could not create and return the Library directory.")
        }
        return result
    }

    func sharedContainerURL(forSecurityApplicationGroupIdentifier identifier: String) -> URL {
        guard let result = containerURL(forSecurityApplicationGroupIdentifier: identifier) else {
            fatalError("Could not return the shared container URL for \(identifier).")
        }
        return result
    }
}

What These Directories Are For

  • Documents: User-generated content. Backed up by iCloud. Exposed if file sharing is enabled.
  • Caches: Temporary files. Not backed up. Can be wiped by the system.
  • Library: App-specific support files. Includes Preferences and Caches subfolders.
  • Shared Container: Private container shared between app and its extensions.

User Access: What’s Visible?

Users can access only if you explicitly expose:

  • Files via UIDocumentPickerViewController or share sheets
  • Content through a custom UI inside your app
  • Files via the Files app, if you implement a File Provider extension

Users cannot access:

  • The shared container
  • Any directory without your explicit sharing
  • Files through Finder, iTunes File Sharing, or third-party managers (on non-jailbroken devices)

Why Use sharedContainerURL?

App Groups are secure containers that:

  • Are sandboxed from user access
  • Allow seamless sharing between your app and extensions
  • Are ideal for sharing login tokens, configuration, or caches

Example

1
2
3
4
let sharedContainer = FileManager.default.sharedContainerURL(
    forSecurityApplicationGroupIdentifier: "group.com.example.myapp"
)
let configFile = sharedContainer.appendingPathComponent("config.json")

This keeps your app’s private data shared and secure—without needing complex logic or risking accidental exposure.

Conclusion

Use this FileManager extension to simplify directory access in your iOS projects. It enforces best practices by failing early and makes your intent clear: whether saving caches, documents, or securely sharing data between your app and extensions.

Avoid hardcoding paths or reusing verbose code. Make sandbox navigation safe, clean, and efficient.

☕ Support My Work

If you found this post helpful and want to support more content like this, you can buy me a coffee!

Your support helps me continue creating useful articles and tips for fellow developers. Thank you! 🙏

This post is licensed under CC BY 4.0 by the author.