Chapter 21

Basic File Handling

Your iPhone applications always execute in a restricted environment on the device known as the application sandbox. Some of these restrictions affect where and how your application can store data. Several types of applications need the ability to store user-created data. Take, for instance, the Notes application. The notes that the user creates with this application need to be stored somewhere so that they are available when the application is restarted.

You can store data in several ways on an iOS device. In this lesson you learn to store data within files on the device.

The IOS File System

Each application is given a directory on the device’s file system. The contents of this directory are private to the application, and cannot be read by other applications on the device. The information from some of Apple’s applications such as Photos and Contacts can be accessed by third-party applications using frameworks in the iOS SDK.

Each application’s directory has four locations into which you can store data:

  • Preferences
  • Documents
  • Caches
  • tmp

The first of these, Preferences, is not intended for direct file manipulation; however, the other three are. The most commonly used directories are the Documents and the tmp directories.

The Documents directory is the main location for storing application data. The contents of this directory can also be manipulated within iTunes (this is covered in Lesson 24). The Caches directory is used to store temporary files ...

Get iPhone and iPad App 24-Hour Trainer now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.