• About Us
    • New York
  • Work
  • Capabilities
  • Careers
  • Technology
  • Blog
  • Contact Us
  • About Us
    • New York
  • Work
  • Capabilities
  • Careers
  • Technology
  • Blog
  • Contact Us
June 21, 2012

Storing Data On Mobile Platforms: iPhone

Posted by PMG Advertising Agency

In almost every mobile application you will have need to store data, whether it be to preserve state, to save information for the user, or to cache data received from a web service for “offline mode”, it is almost inevitable. Depending on your applications specific needs, there are many different options you can choose from, when it comes to storing data on the iPhone.

NSUserDefaults

First on the list is NSUserDefaults. The NSUserDefaults class is actually based on a form of data storage called a plist, which we’ll cover in a moment, and is a great way to store simple data. For instance, you could save whether a user wants sound effects in the app, and the object will be saved in what is known as the iOS “defaults system”. The iOS defaults system is accessible all throughout the code of your app, and any data saved to it will persist through all application sessions.

Being based on plists NSUserDefaults is limited to storing only objects from these classes: NSData, NSString, NSNumber, NSDate, NSArray, and NSDictionary. As such, NSUserDefaults is not well suited to storing complex model objects, or large sets of data. It is best used as a means to store lightweight app state or user settings.

Saving data to NSUserDefaults

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

[prefs setObject:@”Text to be saved” forKey:@”NSStringKey”];
[prefs setInteger:101 forKey:@”NSIntegerKey”];
[prefs setFloat:9.87 forKey:@”floatKey”];

[prefs synchronize];

Retrieving data from NSUserDefaults

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

NSString *storedString = [prefs stringForKey:@”NSStringKey”];
NSInteger storedNSInteger = [prefs integerForKey:@”NSIntegerKey”];
float storedFloat = [prefs floatForKey:@”floatKey”];

Plist

If you’ve done very much development in Xcode, I can guarantee that you’ve worked with at least one plist. Granted, you didn’t have to write the code to actually access it’s data, you’ve certainly worked with the info.plist file in an Xcode project.

A plist is actually just an xml file, which, same as any xml file, you can use to store data. Since it doesn’t have the wrapper of the NSUserDefaults class, using a custom plist is actually faster than using NSUserDefaults. Though, it is still restricted in the types of data it can hold, and a plist is not really suitable for storing complex data objects.

In order to use a plist, you first have to create a new .plist file in your project. Then, before attempting to save or retrieve data from your custom plist you’ll want to execute this:

NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path;
path = [documentsDirectory stringByAppendingPathComponent:@"data.plist"];

NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath: path]) {
NSString *bundle;
bundle = [[NSBundle mainBundle] pathForResource:@”data” ofType:@”plist”];
[fileManager copyItemAtPath:bundle toPath: path error:&error];
}

You have to do this, before anything else, because the original file, created in your project, is stored in the bundle directory. The bundle directory, is a read-only directory, so you can’t actually save data into the original plist you created in the bundle directory. In order to save to it, you have to create a copy of the .plist file in the document directory, where iOS permissions will allow you to read and write on the file.

Now, writing data to a Plist

NSMutableDictionary *data;
data = [[NSMutableDictionary alloc] initWithContentsOfFile:path];

NSString *toStoreString = @”Text to be saved”;
[data setObject:toStoreString forKey:@”NSStringKey”];
[data setObject:[NSNumber numberWithInt:101] forKey:@”NSIntegerKey”];
[data setObject:[NSNumber numberWithFloat:9.87] forKey:@”floatKey”];

[data writeToFile:path atomically:YES];
[data release];

Reading data from a Plist

NSMutableDictionary *savedStock;
savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile:path];

NSString *storedString;
NSInteger storedNSInteger;
float storedFloat;
storedString = (NSString *)[savedStock objectForKey:@”NSStringKey”];
storedNSInteger = [[savedStock objectForKey:@”NSIntegerKey”] intValue];
storedFloat = [[savedStock objectForKey:@”floatKey”] floatValue];

[savedStock release];

SQLite

SQLite is a relational database management system contained in a small C library that is implemented as a self-contained, serverless, zero-configuration, transactional SQL database engine. In contrast to most other database systems, SQLite is not a separate process that is accessed from the application, but is actually integrated into it. SQLite is a popular choice as an embedded database for data storage in applications, and is arguably the most widely deployed database engine. Also, all of the source code for SQLite is in the public domain.

When storing complex model objects, SQLite is far better suited to managing that data than a Plist, though, setting up an SQLite database for storing your model data is quite a lot more complex than setting up a Plist. If you want to learn how to access a SQLite database in Objective-C, you can read this iPhone SDK Tutorial: Reading Data from a SQLite Database.

Core Data

Core Data is an Apple framework described as a “schema-driven object graph management and persistence framework.” The framework manages where data is stored, how it is stored, data caching, and memory management. It was initially written for Mac OS X and was ported to the iPhone for the release of iOS 3.0.

Core Data allows you to create and use a relational database, perform record validation, and perform queries without using SQL conditions. It essentially allows you to interact with SQLite in Objective-C and not have to worry about connections or managing the database schema. The main plus of using Core Data is that it eliminates the need to develop and write complex SQL queries or manually handle the SQL and output of those operations.

Again, using Core Data is a far more complex process than using a Plist, if you want to learn how, you can use this Core Data Tutorial for iOS: Introduction

In Closing

This is not meant to be an explanation of the very best option for data storage in iOS, and why it is the absolute best. Every option has its own set of advantages, as well as its own issues. What option you choose has to be entirely based upon your own application’s specific needs.

Post Image by GDS Infographics

app developmentios app developmentiOS appsios data storage
Previous
Next

Latest White Papers

  • Shifting Plans for 2020 & Beyond
  • Game On: How Brands Can Log Into A Diverse Multi-Billion Dollar Industry
  • What CCPA Means For Brands
  • How Google is Improving Consumer Data Privacy
  • Ways to Prepare for the Cookieless Future
  • See all White Papers

Featured Posts

  • Ad Age Names PMG #1 Best Place to Work in 2021
  • Hindsight 2020 & Looking Ahead to 2021
  • Preparing for Streaming’s Growth & The Future of TV Buying
  • MediaPost Names PMG Independent Agency of the Year
  • PMG Client Portfolio Trends During Amazon Prime Day 2020

Categories

  • Consumer Insights
  • Content
  • Creative Design
  • Data Analytics
  • Development
  • Digital TV & Video
  • Ecommerce
  • Industry News
  • Local
  • Mobile
  • Paid Search
  • PMG Culture
  • Programmatic & Display
  • SEO
  • Social Media
  • Structured Data
Fort Worth

2845 West 7th Street
Fort Worth, TX 76107

Dallas

3102 Oak Lawn Avenue
Suite 650
Dallas, TX 75219

Austin

823 Congress Avenue
Suite 800
Austin, TX 78701

London

33 Broadwick Street
London
W1F 0DQ

New York

120 East 23rd Street
New York, NY 10010

Get in touch

(817) 420 9970
info@pmg.com

Subscribe to the PMG Newsletter
© 2021 PMG Worldwide, LLC, All Rights Reserved
  • Contact
  • Privacy Policy
 Tweet
 Share
 Tweet
 Share
 Tweet
 Share
 LinkedIn
We and our partners use cookies to personalize content, analyze traffic, and deliver ads. By using our website, you agree to the use of cookies as described in our Cookie Policy.