The Application Sandbox

Every iOS application you create is placed in its own sandbox. A sandbox is a directory on the iOS file system. A sandbox holds your application’s executable file, which includes all embedded resources. Further more, an application sandbox contain several folders, and you can read and write files in those listed in the table below. You will mostly read and write files in the Documents and tmp folder.

Caches This directory is where you write data that the application generates during runtime and that you want to persist between runs of the application. However, unlike the Documents directory, it does not get backed up when the device is synchronized with iTunes. A major reason for not backing up cached data is that the data can be very large and extend the time it takes to synchronize your device. Data stored somewhere else – like a web server – can be placed in this directory. If the user needs to restore the device, this data can be downloaded from the web server again.
Preferences This directory is where any preferences are stored and where the Settings application looks for application preferences. Preferences is handled automatically by the class NSUserDefaults and is backed up when the device is synchronized with iTunes.
Documents This directory is where you write data that the application generates during runtime and that you want to persist between runs of the application. For example, you can store databases that are required by your application. Data stored in this folder is backed up when the device is synchronized with iTunes. If something goes wrong with the device, files in this directory can be restored from iTunes.
tmp This directory is where you write data that you will use temporarily during an application’s runtime. For example, results returned from a web service that you will store somewhere else later, images, etc. Files placed in the tmp folder are temporary and should be deleted when an application terminates. If the application does not clean the folder, iOS might delete them depending on when space is needed on your device. It does not get backed up when the device is synchronized with iTunes.

An application can access only the files stored within its own sandbox; it cannot access or create files outside of its sandbox. A sandbox’s file name (FDDE4989-BD9F…) is generated by Xcode to uniquely identify the application’s sandbox. Here is a visual of typical application sandbox. The iOS application’s name is MyApp.

appsandbox_fig1

Revealing The Hidden Library Folder

The Library folder is hidden in the Lion OS by default, so do the following to reveal it:

1. Launch Finder and select the Go to Folder… menu item.

appsandbox_fig2

2. Enter ~/Library in the input box that pops up, before clicking the Go button.

appsandbox_fig3

3. Once you click the Go button, the Library folder becomes visible as shown in this image.

appsandbox_fig4

Accessing an Application Sandbox

All application sandboxes are stored in the ../Library/…/Applications folder on your Mac computer. So you access an application’s sandbox by drilling down to the Applications folder as shown in this image.

appsandbox_fig5

Within the Applications folder you’ll find one or more application sandboxes, as shown in this image.

appsandbox_fig6

Every time you create an iOS application, the system automatically creates a sandbox for it in the Applications folder.