Mobile Development

Local Storage

iOS provides two convenient and easy ways to store data that will persist across app launches. Those two ways are:

  1. NSUserDefaults
  2. plist files

These are paradigms typically used for configuration-level settings.

NSUserDefaults

NSUserDefaults persist between app loads and are useful for non-secure configuration settings.

The NSUserDefaults class provides a programmatic interface for interacting with the defaults system. The defaults system allows an application to customize its behavior to match a user’s preferences

Reading from NSUserDefaults

import Foundation

let name = "Jeff"
let key = "Name"

var defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(name, forKey: key)
defaults.synchronize() // save our defaults

Note, to save NSUserDefaults to disk, the function synchronize() must be called.

Writing to NSUserDefaults

import Foundation

let name = "Jeff"

if let usersName : AnyObject? = defaults.objectForKey(key) {
    NSLog("Hey \(usersName)!")
}

You can store a variety of information to NSUserDefaults including arrays, dictionaries, custom objects and scalar types such as integers and floats.

plists

Property Lists (or plists) are XML-based files that are usually contained within your application bundle. They are called plists because of their file extension, which is *.plist. The application bundle is a set of files which comprise imagery, other plists and your application executable.

Reading from a plist file

To read from a plist file, we first get a reference into our main bundle object. Then we copy the contents of that file into a dictionary object. Once we have the dictionary object, we can retrieve a particular value:

import Foundation

var textSize = 12.0

let path = NSBundle.mainBundle().pathForResource("AppSettings", ofType: "plist")

if let dict = NSDictionary(contentsOfFile: path!) {
    textSize = dict.objectForKey("Preferred Text Size") as Double
    NSLog("Preferred text size is \(textSize) points")
}
else {
    NSLog("Preferred text is not available, defaulting to \(textSize) points")
}

Writing to a plist file

Writing to a plist follows the same conventions as above. The only difference is once we have access to the dictionary, we simply need to write to the dictionary as opposed to read from it. Additionally, we also need to save that file back to disk.

import Foundation

var newTextSize = 14.0

let path = NSBundle.mainBundle().pathForResource("AppSettings", ofType: "plist")

if let dict = NSDictionary(contentsOfFile: path!) {
    dict.setValue(newTextSize, forKey: "Preferred Text Size")
    NSLog("Preferred text size is now \(textSize) points")
    dict.writeToFile(path!, atomically: false)
}
else {
    NSLog("Preferred text is not writable")
}

For more robust data storage options, read on as we walk through storing data in files and managing a Core Data database.

References

[1] https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/index.html

[2] https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/AboutInformationPropertyListFiles.html