Introduction to data storage on an iOS device using a plist

Your app can read and write to the file system of the iOS device. Simple data persistence can be done with a “property list”, also known as a plist. It is typically an XML-based storage format, for certain kinds of objects. Strings, numbers, arrays, and dictionaries can easily be stored in a plist.

.

This document was most recently updated in January, 2014.

.

Data storage on an iOS device

Your app can read and write to the file system of the iOS device:

  • It can read from the app bundle, or the app’s “Documents” directory
  • It can write to the app’s “Documents” directory

We use an NSURL object to define a file system path.

.

Reading data 

If your app needs data when launched for the first time on a device, you must provide it.

You can also generate the data, or fetch it from the network. We’ll do that in a few weeks.

.

How do you provide the data? Include it in the app bundle. In other words, include it with your project files.

Your app and all of its required resources – images, configuration data, etc. – are packaged in a “bundle” when you build/compile your project. This app bundle can then be deployed/installed on an iOS device.

iOS provides a programming interface to enable your code to locate and read objects in the app bundle.

If you’re curious, more information is found in the Bundle Programming Guide.

.

When your app is running, you can read from the app bundle at any time (not just when the app launches for the first time).

You CANNOT write to the app bundle.

How do you create the URL to a file in the app bundle? The following code creates a URL to “examplePhoto.png”:

NSURL *urlToFile = [[NSBundle mainBundle] URLForResource:@"examplePhoto" withExtension:@"png"];

.

Writing data

Can you write/save data? Yes. Where? In your app’s “Documents” directory.

What data formats can you write (and read) from the app’s Documents directory? We’ll study two data formats in this course:

  1. Simple property list, known as a “plist”
  2. Object store, managed by the Core Data framework (which we’ll begin learning soon)

.

The URL to the app’s Documents directory, and to a file named “settings.plist” within that directory, is created with this code:

// Get a reference to the file system
NSFileManager *fs = [[NSFileManager alloc] init];
// URL to Documents directory
NSURL *urlToDocumentsDirectory = [[fs URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] firstObject];
// URL to a data file named 'settings.plist' in that directory
NSURL *urlToFile = [urlToDocumentsDirectory URLByAppendingPathComponent:@"settings.plist"];
// Now you can use 'urlToFile' to reference the data file

:

.

Getting started with a plist (property list)

As noted above, simple data persistence can be done with a “property list”, also known as a plist. It is typically a plain-text XML-based storage format, for certain kinds of objects. Strings, numbers, arrays, and dictionaries can easily be stored in a plist.

The following image shows a sample plist, from the Table Save example app:

plist-example

.

Array – write to file system, read from file system

An array can be “round tripped”as a plist.

Assuming that “myArray” is an array object, and “urlToFile” is a complete file URL, here’s how you create/write a plist for the array:

[myArray writeToURL:urlToFile atomically:YES];

.

Here’s how you open/read a plist into an array (again, assuming “urlToFile” is a file URL…):

NSArray *myArray = [NSArray arrayWithContentsOfURL:urlToFile];

.

Dictionary – write to file system, read from file system

A dictionary can also be “round tripped” as a plist.

Assume “myDict” is a dictionary object; and again, assuming “urlToFile”, here’s how you create/write a plist for the dictionary:

[myDict writeToURL:urlToFile atomically:YES];

.

Here’s how you open/read a plist into a dictionary (again, assuming “urlToFile”…):

NSDictionary *myDict = [NSDictionary dictionaryWithContentsOfURL:urlToFile];

.

Allowable data types in a plist

The following types can be stored in a plist:

  • NSDictionary
  • NSArray
  • NSString
  • NSNumber
  • NSData
  • NSDate

Nesting of NSDictionary and NSArray objects is permitted. This enables you to easily persist a hierarchical data structure.

Complete coverage is in the Property List Programming Guide.

.

If you want to know more about working with the file system…

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

In Finder, on the “Go” menu, choose “Go to Folder…” (or press Shift+Command+G). Enter this path:

~/Library

Navigate through these folders:

Application Support > iPhone Simulator > [ version number ] > Applications

Each application is located in a folder with a “unique app ID” name.

The following image shows the file system objects inside an app named “Collections”:

.

.

.

.

.

.

.

.

.

.

.

  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: