Write a file on iOS
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Writing a file on iOS usually comes down to three decisions: which sandbox directory to use, whether you are writing text or binary data, and how you want to handle errors. The actual API surface is small once those choices are clear.
In modern iOS code, FileManager, String.write, and Data.write cover most file-writing tasks. The harder part is choosing the right location so the file behaves correctly with backup, caching, and app restarts.
Choose the right sandbox directory
Every iOS app runs in its own sandbox. You cannot write arbitrary files anywhere on the device, only inside directories your app owns.
The most common choices are:
- '
Documentsfor user-generated or user-visible files' - '
Library/Application Supportfor internal app data that should persist' - '
Library/Cachesfor data that can be recreated' - '
tmpfor short-lived temporary files'
If the file should survive app launches and matter to the user, Documents is usually correct. If it is internal state, Application Support is often better.
Write a text file with String.write
For simple text output, use String.write. First, build the destination URL from FileManager.
This creates or overwrites notes.txt in the app's Documents directory. The atomically flag writes to a temporary file first and then swaps it into place, which reduces the risk of leaving a partially written file behind.
Write binary data with Data.write
If you are saving JSON bytes, images, or custom binary content, write Data instead of String.
This pattern is common for app preferences, cached API responses, and offline state.
Create directories before writing nested files
If your destination lives inside a subdirectory, create that folder first:
Without that directory creation step, the write fails because the parent folder does not exist.
Think about backup and privacy
Where you write the file affects system behavior. Files in Documents and much of Library may be backed up. Cache files and temporary files may be deleted by the system when space is needed.
For sensitive files, the sandbox alone may not be enough. iOS data protection, Keychain storage, or encryption may be more appropriate depending on what the file contains.
Common Pitfalls
- Saving internal cache-like data in
Documentsand causing unnecessary user-facing persistence or backups. - Building file paths as raw strings instead of using
URLandFileManager. - Forgetting to create parent directories before writing nested files.
- Doing large or repeated file I/O on the main thread and stalling the UI.
- Treating sandboxing alone as enough protection for sensitive data that may need stronger storage controls.
Summary
- Use the app sandbox and choose the directory based on the file's purpose.
- '
String.writeis convenient for text files, whileData.writeis better for binary or encoded content.' - Build file paths with
URL, not manual string concatenation. - Create parent directories before writing nested files.
- Treat backup, caching, and privacy as part of the file-writing decision, not as afterthoughts.
Related reading
- Write applications in C or C for Android?
- Writing handler for UIAlertAction
- Writing handler for UIAlertAction
- Wrong requestCode in onActivityResult
- Xamarin Gcm Network Manager await httpclient
- Xamarin.Android no stack trace in async method
- Xamarin.Forms ListView Set the highlight color of a tapped item
- xcode-select active developer directory error
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.