DISCLAIMER: This post is older than one year and maybe out of date.

How To Save and Load Your iOS Application Settings Using UserDefaults in Swift 3

Most applications you write, regardless of type will require the saving and loading of user settings at some point. If that data is simple and straight forward then you can use UserDefaults to achieve this with just a few lines of code. Here’s how.

The Set-Up

For this example, I am going to have a text field you can enter text into that gets saved and restored when the application loads, a switch that will restore it’s on/off state, and finally a button to save the settings.

Create a single view iOS Swift Application and follow along.

Build the Example UI

Add a Text Field, Switch, and Button to the stage, if desired add constraints to keep the UI nice.

Xcode Storyboard with a Text Field, Switch and Button

Now, add an IBOutlet for both the Text Field and the Switch. Lastly, add an IBAction for the Button, these should be in the ViewController.swift.

If you were to run the application now, you could certainly enter text and toggle the switch, but if you closed the application (I mean CLOSE and not just hit the home button to send it to the background), everything would be restored back to the default setting. So let’s add the magic that stores our state.

What is UserDefaults

Before we go any further, I want to briefly talk about what UserDefaults can do for us and how it works.

UserDefaults is much like a plist, a plist is a simple key-value pair file that stores types of data. We give each item a name and value for retrieval later. The following types of data can be stored.

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

As you can see it is clearly designed for small simple chunks of data that you would use for settings and the like.

When we create an instance of the UserDefaults object, it returns to us what is essentially a singleton as each subsequent reference returns back that same instance rather than creating a new one.

Enough information, on with the show that you probably came here for!

A little planning is required here as to what we need to do, so let’s make a requirement list of what we need the app to do and when.

  • We need to allow for the first run experience when there is no settings file.
  • We want to load the settings when the app loads and set the UI up if any settings exist.
  • We want to save the settings when the button is clicked.

OK, so time to code up the requirements.

Load Our Settings on Application Load

First, we need to get access to NSUserDefault, we will do that in the viewDidLoad() for this example. Since we want to be a good Swift citizen we will use a let rather than var.

Since we would expect to use the key names for reading and writing, we want to minimize the chances of typo’s. To do that we will use constants for them. Just inside the class, add two constants, one for the text field key name, and another for the switch key name.

Now we can use those and never have to worry about typing a name wrong.

Next back inside the viewDidLoad() after our defaults line, we will get and assign the values. BUT we need to practice safe coding, so we will check that there is a value to load.

Now we will add the switch. Since it simply needs a boolean to turn either on or off, we can get the value and set the switch a different way.

That is the reading and UI setup completed, now the last step is to wire up the button to save the settings.

Save Our Settings with the Button

Inside the function we assigned to the button, we again get a reference to our UserDefaults and then set values for the two key names.

Notice we use two different methods for storing the values, one for a string and another for a boolean. Syntax autocomplete will provide you a list to choose from, so be sure to pick the right one for the value type you are using.

So that is our coding completed, running the application and touching the button will store the values you set each time the application is run (again, do not confuse going to the background with starting the application).

Here is the complete code for the viewController.swift in my application.

The Wrap

I hope this gives you an insight as to how easy it is to store settings for your applications. As you can see the code involved is only a few lines.

The same method works for Swift 2, only the names of some methods and objects have changed. For example UserDefaults in Swift 2 is NSUserDefaults.