O'Reilly logo

Learning iPhone Programming by Alasdair Allan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Storing Data

If the user creates data while running your application, you may need a place to store the data so that it’s there the next time the user runs it. You’ll also want to store user preferences, passwords, and many other forms of data. You could store data online somewhere, but then your application won’t function unless it’s online. The iPhone can store data in lots of ways.

Using Flat Files

So-called flat files are files that contain data, but are typically not backed by the power of a full-featured database system. They are useful for storing small bits of text data, but they lack the performance and organizational advantages that a database provides.

Applications running on the iPhone or iPod touch are sandboxed; you can access only a limited subset of the filesystem from your application. If you want to save files from your application, you should save them into the application’s Document directory.

Here’s the code you need to locate the application’s Document directory:

NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(
  NSDocumentDirectory,   NSUserDomainMask, YES);
NSString *docDirectory = [arrayPaths objectAtIndex:0];1
1

The first entry in the array will contain the file path to the application’s Document directory.

Reading and writing text content

The NSFileManager methods ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required