Show Menu

Looking to hire an app developer?

Submit your 30 day Job Listing for FREE

How can you store data between application launches?

There are plenty of ways, but here I will show you the simplest, NSUserDefaults.

Objective-C NSUserDefaults

Just so you know, This article is about using NSUserDefaults in Objective-C – There is an pdated version for Swift here: Swift NSUserDefaults

NSUserDefaults is a simple property list (aka plist) where an app can store simple data. While there is no limit to its size (besides the device’s own limits), you should not store a large amount of data here. The file is written and read atomically (as a whole), so the more data that is in there, the longer that will take. Nonetheless, it is a great place to store settings, high scores, and the like.

Intro to Property Lists

Property lists can only accept certain types of variables, so NSUserDefaults has that limitation. You can store these types of variables:

– NSArray
– NSData
– NSDictionary
– NSNumber
– NSString

Furthermore, the NSArray or NSDictionary must only contain the types listed above (yes, nesting of NSArray or NSDictionary is allowed). Other items that conform to the NSCoding protocol can be archived as NSData, so you can store them in property lists if necessary. I have not really looked in to NSCoding yet, so that will be the subject of some future post. Also, the keys for the root of the property list, as well as any NSDictionary stored inside, must be NSStrings.

Saving to NSUserDefaults

Basically, all you have to do is load NSUserDefaults, and then tell it to save a value to a specific key. Here is a simple example of writing an integer to NSUserDefaults:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:9001 forKey:@"HighScore"];
[defaults synchronize];

As mentioned earlier, we loaded the NSUserDefaults plist to the object “defaults”. We then told that object to set an integer for the key “HighScore”. That final command to defaults to “synchronize” is basically to force a save. It isn’t absolutely necessary, because this is called automatically every so often, but if you want to be sure to save the changes to NSUserDefaults right now, this is how.

Pretty simple, eh?

Reading from NSUserDefaults

Reading is even easier, as shown below:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSInteger theHighScore = [defaults integerForKey:@"HighScore"];

That’s all there is to it. You just load NSUserDefaults, then tell it to retrieve a type for a specific key. No need to synchronize or anything here, so one less line of code than saving.

One extra little tidbit here, you will notice that NSInteger has no asterisk after the type like NSUserDefaults does. That is because NSInteger is actually just a typecast of long or int, depending on whether the system it is running on is 64-bit or 32-bit respectively, and not a normal Objective-C object.

You can read more on how to store different values in the documentation from Apple, but this is enough to get you started

having issues?

We have a Questions and Answer section where you can ask your iOS Development questions to thousands of iOS Developers.

Ask Question

FREE Download!

Get your FREE Swift 2 Cheat Sheet and quick reference guide PDF download when you sign up to SwiftMonthly

Sharing is caring

If you enjoyed this tutorial, please help us and others by sharing using one of the social media buttons below.

Written by:

Nick Hanan is learning Objective-C and wants to help others on the same journey. He started the Coding Explorer Blog to chronicle what he is learning as he explores the Objective-C landscape. Check out his blog for information from a beginners perspective