0Pricing
Objective-C iOS Development for Legacy & Enterprise Apps · Lesson

Storing Preferences with NSUserDefaults

Persist lightweight settings and user preferences in Objective-C apps using NSUserDefaults, the simplest key-value store on iOS.

Storing Preferences with NSUserDefaults is a free Objective-C iOS Development for Legacy & Enterprise Apps lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Objective-C iOS Development for Legacy & Enterprise Apps learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What is NSUserDefaults?

NSUserDefaults is a built-in key-value store for small pieces of data — settings, flags, and preferences. It persists automatically across app launches.

When to Use It

Use it for small, simple values:

  • User toggles (dark mode on/off)
  • Last selected tab
  • Onboarding-completed flag

Do NOT use it for large data or sensitive secrets.

The Standard Instance

Access the shared store through standardUserDefaults.

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

Writing Values

Set values by key with typed setters.

[defaults setBool:YES forKey:@"darkMode"];
[defaults setInteger:5 forKey:@"launchCount"];
[defaults setObject:@"Ada" forKey:@"username"];

Reading Values

Read with matching getters. Missing keys return sensible defaults (NO, 0, nil).

BOOL dark = [defaults boolForKey:@"darkMode"];
NSInteger count = [defaults integerForKey:@"launchCount"];
NSString *name = [defaults stringForKey:@"username"];

Supported Types

NSUserDefaults stores property-list types only:

  • NSString, NSNumber, BOOL, integers, doubles
  • NSArray, NSDictionary (of plist types)
  • NSData, NSDate

Persistence is Automatic

Modern iOS writes changes to disk automatically. The old synchronize call is no longer needed and is deprecated.

// [defaults synchronize]; // no longer required

Registering Default Values

Provide fallback values for first launch with registerDefaults:. These apply only until the user changes them.

[defaults registerDefaults:@{@"darkMode": @NO, @"fontSize": @14}];

Removing a Value

Delete a stored key when it is no longer needed.

[defaults removeObjectForKey:@"username"];

Storing Custom Objects

To store a custom object, archive it to NSData first (it must conform to NSCoding), then save that data. NSUserDefaults cannot hold arbitrary objects directly.

Security Caveat

NSUserDefaults is not encrypted. Never store passwords, tokens, or other secrets there — use the Keychain for sensitive data.

Quick Check

Test your NSUserDefaults knowledge.

Recap

You learned NSUserDefaults persistence:

  • A simple key-value store for small preferences
  • Write with setBool:/setObject:, read with matching getters
  • Stores only property-list types
  • Persistence is automatic; synchronize is deprecated
  • Not encrypted — keep secrets in the Keychain

It is the go-to for lightweight settings.

Frequently asked questions

Is the “Storing Preferences with NSUserDefaults” lesson free?

Yes — the full text of “Storing Preferences with NSUserDefaults” is free to read here on the web, and the Objective-C iOS Development for Legacy & Enterprise Apps course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Objective-C iOS Development for Legacy & Enterprise Apps course, upgrade to CoddyKit PRO.

What will I learn in “Storing Preferences with NSUserDefaults”?

Persist lightweight settings and user preferences in Objective-C apps using NSUserDefaults, the simplest key-value store on iOS. You practise Objective-C iOS Development for Legacy & Enterprise Apps with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Objective-C iOS Development for Legacy & Enterprise Apps?

No prior experience is required. Objective-C iOS Development for Legacy & Enterprise Apps on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Storing Preferences with NSUserDefaults” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Objective-C iOS Development for Legacy & Enterprise Apps lesson?

Yes. Every Objective-C iOS Development for Legacy & Enterprise Apps lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Property Lists and Archiving
  2. Core Data Fundamentals
  3. SQLite Integration with FMDB
  4. Storing Preferences with NSUserDefaults
← Back to Objective-C iOS Development for Legacy & Enterprise Apps