temporaryDirectory (iOS)
Also known as: nstemporarydirectory, tmp folder ios
temporaryDirectory is the per-app tmp folder where iOS apps write short-lived working files. It is unique to the app sandbox, excluded from backups, and may be emptied by the system when the app is not running, making it a natural target for both system and in-app cleanup.
- Read it via FileManager.default.temporaryDirectory or NSTemporaryDirectory().
- The tmp folder is excluded from iCloud and computer backups.
- iOS may empty tmp while the app is not running, so files there are never guaranteed to persist.
What the temporary directory is
Every iOS app sandbox contains a tmp folder for data the app does not need to persist between launches, such as intermediate files during a download, export, or media conversion. In Swift apps read its URL from FileManager.default.temporaryDirectory; the older C function NSTemporaryDirectory() returns the same path as a string.
The folder is excluded from iCloud and computer backups, and Apple advises apps to delete temporary files themselves as soon as they are done with them rather than relying on the system.
System and cleaner behavior
iOS may purge the tmp folder when the app is not running, so apps must never assume a temporary file still exists on the next launch. This makes tmp similar to Library/Caches in that it holds reclaimable, non-essential data, though apps are expected to be more aggressive about clearing tmp themselves.
A cleaner can enumerate the app's temporary directory with FileManager and call removeItem(at:) to free stale working files immediately instead of waiting for the system to purge them. As with all sandbox folders, it can only clear its own tmp, not other apps'. To create a uniquely named scratch space, apps use url(for: .itemReplacementDirectory, ...).