Swift Academy · Lesson

App Sandbox Directories

Use Documents, Caches, and temporary locations.

Lesson 4 of 413 steps

App Sandbox Directories is a free Swift Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

App Sandbox Directories

Apps run in a sandbox: they may only write inside their own container. Standard folders exist for documents, caches, and temporary files.

Finding the Documents Directory

FileManager.urls(for:in:) returns the URLs for a known directory. .documentDirectory holds user data that should be backed up.

import Foundation

let fm = FileManager.default
let docs = fm.urls(for: .documentDirectory, in: .userDomainMask).first!
print("Documents path ends correctly: \(docs.lastPathComponent)")

Building File URLs

Append a filename to the directory URL to get a full path for your own files.

import Foundation

let fm = FileManager.default
let docs = fm.urls(for: .documentDirectory, in: .userDomainMask).first!
let file = docs.appendingPathComponent("profile.json")
print(file.lastPathComponent)

The Caches Directory

.cachesDirectory stores data you can regenerate. The system may purge it under disk pressure, so never keep irreplaceable data here.

import Foundation

let fm = FileManager.default
let caches = fm.urls(for: .cachesDirectory, in: .userDomainMask).first!
print("Caches dir name: \(caches.lastPathComponent)")

The Temporary Directory

FileManager.default.temporaryDirectory gives a scratch space for short-lived files that you should clean up yourself.

import Foundation

let tmp = FileManager.default.temporaryDirectory
let scratch = tmp.appendingPathComponent("work.tmp")
print("Temp file path ready: \(scratch.lastPathComponent)")

Documents vs Caches vs Temp

Use Documents for user-created data (backed up), Caches for reproducible data, and tmp for throwaway files.

import Foundation

// Choose the right folder for the data's lifetime.
print("Documents = backed up, Caches = purgeable, tmp = transient")

Application Support

.applicationSupportDirectory holds app-managed data not directly user-facing, such as databases. You often create it on first launch.

import Foundation

let fm = FileManager.default
let support = fm.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
try? fm.createDirectory(at: support, withIntermediateDirectories: true)
print("Ensured Application Support exists")

Saving to Documents

Combine the directory URL with a write call to persist user data inside the sandbox.

import Foundation

let fm = FileManager.default
let docs = fm.urls(for: .documentDirectory, in: .userDomainMask).first!
let url = docs.appendingPathComponent("note.txt")
try? "hello".write(to: url, atomically: true, encoding: .utf8)
print("Saved into Documents")

Reading Back

Read sandbox files the same way as any file URL, since the path is fully valid within the container.

import Foundation

let fm = FileManager.default
let docs = fm.urls(for: .documentDirectory, in: .userDomainMask).first!
let url = docs.appendingPathComponent("note.txt")
if let text = try? String(contentsOf: url, encoding: .utf8) {
    print(text)
}

Excluding from Backup

Set the isExcludedFromBackup resource value on large regenerable files so they do not bloat iCloud backups.

import Foundation

var url = URL(fileURLWithPath: "/tmp/big.cache")
var values = URLResourceValues()
values.isExcludedFromBackup = true
try? url.setResourceValues(values)
print("Marked excluded from backup")

Why Sandboxing Matters

Sandbox paths differ per install and per device, so never hardcode them. Always resolve directories at runtime with FileManager.

import Foundation

// Never hardcode /var/mobile/... — resolve at runtime instead.
let fm = FileManager.default
let docs = fm.urls(for: .documentDirectory, in: .userDomainMask).first!
print("Resolved Documents dynamically: \(!docs.path.isEmpty)")

Quick Check

Which directory should hold user data that must be backed up?

Recap

Apps store files in sandbox folders resolved via FileManager.urls(for:in:): Documents for backed-up user data, Caches for regenerable data, tmp for transient files, plus Application Support. Resolve paths at runtime, never hardcode them. That completes the File I/O course.

Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “App Sandbox Directories” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “App Sandbox Directories”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

What will I learn in “App Sandbox Directories”?

Use Documents, Caches, and temporary locations. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “App Sandbox Directories” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. URLs and File Paths
  2. Reading and Writing Data
  3. Managing Directories
  4. App Sandbox Directories
← Back to Swift Academy