Home > 2010 Winter DPS913 > Options for persisting data on an iPhone OS device

Options for persisting data on an iPhone OS device

January 28, 2010 Leave a comment Go to comments


Now you’re ready to save and persist data for your iPhone OS app. What choices do you have? For students in the DPS913 / APD601 class, the choices are covered here.

An iPhone OS device is a computer. Our iPhone OS apps can work with the device’s file system to persist data.

The file system is UNIX-based. Each iPhone OS app runs in an isolated runtime environment. Additionally, the part of the file system that the app has access to is isolated. An app can work only with data in its own app bundle (i.e. the app package that gets installed), or its isolated file system portion. One app cannot get access to another app’s file system portion, or to the device’s system portion.

When an iPhone OS app is installed on the device (or on the iPhone Simulator), the app’s file system storage area is empty. Therefore, if you need to provide getting-started data for your app, then you need to do one or more of the following:

  • Generate the data programmatically, on first launch/run
  • Deliver the data in your app bundle, and programmatically copy it to the file system area on first launch/run
  • Obtain the data from a server, using the device’s network connection

Each app’s file system area is organized as follows:

  • It’s “root” contains the app bundle, and three folders…
  • The Documents folder is to be used for storage of the app’s data; it gets backed up (via iTumes)
  • The Library folder is to be used for preferences storage and a few other purposes; it gets backed up
  • The tmp folder is to be used for “temporary” file system operations


Locating your app’s file system area when using iPhone Simulator

If you are using the iPhone Simulator with Xcode, you can use Finder to locate your app’s file system area. Here’s how:

(your home) > Library > Application Support > iPhone Simulator > User > Applications > (unique app ID)

Each application is located in a folder with a “unique app ID” name. Xcode works with the iPhone Simulator, and the result is a name like this: 1B999F52-8B09-416A-A94B-5B4CE9C98697


Access the file system area programmatically

There are two example apps with fully-working code to access the file system area. In this section, the highlights are presented.

Access the app bundle

You can access the app bundle with the Foundation kit NSBundle class. When called with the correct method, it returns a string with the directory path to the app bundle, and the data file in it that you want.

For example, here’s a complete path plus filename string for a data file located in an app bundle:

/Users/peter/Library/Application Support/iPhone Simulator/User/Applications/FE73661B-8112-4B7D-8409-6808EA30B532/Fall2009Test1.app/NFLscores.plist

If you’re new to the iPhone OS or the Mac OS, an app bundle appears in Finder with a generic icon (or a custom icon if the programmer has provided one), and a name that matches the app’s name. So, to us, it appears as a single icon object. In reality. an app bundle is a directory, with a number of files and folders within it. In Finder, you can right-click the app bundle icon, and “Show Package Contents” to see the files and folders.

Access the app’s Documents folder

Cocoa has a Foundation kit function called NSSearchPathForDirectoriesInDomain (which can also be used in Mac OS X apps). When called with the correct arguments, it returns an array of directory path strings. On iPhone OS 3.1, the array has only one element – a string, which is the full path to the Documents folder.

Notably, there is a convenient NSString method for appending a file name to the end of a directory path string. The result is well-formed, with the right number of slashes and so on.

For example, here’s a complete path plus filename string for the data file used in today’s “Notes plist” app:

/Users/peter/Library/Application Support/iPhone Simulator/User/Applications/880327CC-9E38-4396-80A0-6BB6B4C64774/Documents/Notes.plist


Options for persisting data

iPhone OS offers a number of options for persisting data. We will learn four ways in this course:

  1. Property list (plist)
  2. Archiving
  3. Core Data
  4. Storage located on a sever, accessed over a network, using an API

Today, we will work with the first two options: Property list, and Archiving.

In two weeks, we will work with Core Data. After the study week break, we will work with sever-via-network storage.


Categories: 2010 Winter DPS913
  1. Ryan Hayes
    September 30, 2010 at 2:19 pm

    this is really useful information for getting to the simulator files. I’m trying to get access to the application files from an embedded mobile safari browser. Does anyone know what the paths are to my local resources such as images, css, and javascript files that I packaged with the native app? It would be really convenient to package our bigger files in the app so that they download them when installing the app rather than pulling them up from a server at runtime – this tends to be really slow over 3G.

    • petermcintyre
      September 30, 2010 at 2:29 pm

      I haven’t done what you want exactly, but I think it can be done. The app package is [NSBundle mainbundle]. It would be preferable to copy the necessary resources to a new folder in your documents directory when the app launches for the first time on the device. Then, the UIWebView can open the page in your app’s “file system”, instead of from the app package / bundle.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: