Home > 2010 Fall DPS913 > Options for persisting data on an iOS device

Options for persisting data on an iOS device

September 23, 2010 Leave a comment Go to comments

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

.

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

The file system is UNIX-based. Each iOS 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 iOS 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 > [ version ] > 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/[version]/Applications/FE73661B-8112-4B7D-8409-6808EA30B532/Fall2009Test1.app/NFLscores.plist

If you’re new to the iOS 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 iOS, 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/[version]/Applications/880327CC-9E38-4396-80A0-6BB6B4C64774/Documents/Notes.plist

.

Options for persisting data

iOS 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 server, accessed over a network, using an web services 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 server-via-network web services based storage.

.


Advertisements
Categories: 2010 Fall DPS913
  1. No comments yet.
  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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: