FileManager directories: Documents vs Application Support vs tmp vs Caches


Greetings, traveler!

When saving data in the file system, we have various options. While the Documents directory has been a traditional choice, it’s essential to be aware of other options, each with unique characteristics.

By the way, Swift allows us to save data using not only the file system but also other tools. I invite you to read more about it here.

Documents/

Since Documents has been a preferred option for a long time, let’s start with it. The files you place in this directory can be accessed by the user inside the Files application. For the user to use these files in the Files application, the following lines must be added to the Info.plist:

  • Supports opening documents in place: YES
  • Application supports iTunes file sharing: YES

They can also be exposed via the spotlight. The data in this folder will be backed up using iCloud. But you can exclude them from backup like this:

var fileURL: URL = ... // your file url

var resourceValues = URLResourceValues()
resourceValues.isExcludedFromBackup = true

try? fileURL.setResourceValues(resourceValues)

Apple recommends excluding any file that can be re-created or downloaded from the backup. Remembering to exclude large media files like audio and video would be the best.

You can get the Documents directory URL like this:

let documentDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first

Library/Application support/

It is better to place files that should not be shown to the user here. The data in this folder will also be backed up using iCloud, so remember the backup management rules we discussed. The disk space used in this directory by your app will be visible to the user in the device Settings app via storage report.

Unlike the Documents directory, the Application support directory is not created by default on the device, so you should create it yourself before using it.

You can get the Application support directory URL like this:

let applicationDirectoryURL = FileManager.default.urls(for: .applicationDirectory, in: .userDomainMask).first

tmp/

This directory should be used for data that does not need to persist for an extended period of time because the OS will periodically remove it to free disk space. It won’t happen while your app runs, but you can’t be sure that your files in this directory will be stored long after app termination. This directory won’t be reported in your app’s Documents and Data total, and it won’t be backed up.

You can get the Temporary directory URL like this:

let temporaryDirectoryURL = FileManager.default.temporaryDirectory

Library/Cache/

Like the tmp directory, this one can be cleared by the OS. But this happens more rarely. Since then, your app should be able to recreate these files if needed. This directory is also not tracked by the storage report and won’t be backed up.

You can get the Cache directory URL like this:

let cachesDirectoryURl = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first

Conclusion

That’s all I wanted to say about the iOS File system directories today. I hope you liked this article. See you soon!