User Defaults in Swift

Yafonia Hutabarat
Mac O’Clock
Published in
4 min readMay 11, 2020

All iOS apps have a built in data dictionary that stores small amounts of user settings for as long as the app is installed. This system is called UserDefault.

What is User Defaults?

According to Apple’s documentation, UserDefaults is an interface to the user’s defaults database, where you store key-value pairs persistently across launches of your app.

UserDefault can save integers, booleans, strings, arrays, dictionaries, dates and more, but you should be careful not to save too much data because it will slow the launch of your app.

UserDefaults is just like dictionaries, it consists of keys and values. For example:

var dict = [
"Name": "Yafonia",
"Age" : 21,
"Language": "Indonesian"
]

User defaults are saved in a .plistfile, and in this case is in Info.plist.

UserDefaults on Info.plist file

When to Use User Defaults

The user defaults are best used for simple pieces of data. If you need to store multiple objects, you better use the real database. These are several example pieces of data that are saved in UserDefaults:

  • User information, like name, email address, age, occupation
  • App settings, like user interface language, app color theme or font
  • Flags (isLoggedIn to check whether the user is logged in or not, etc.)

Saving Data in User Defaults

We can save several variable types on UserDefaults:

  • Booleans with Bool, integers with Int, floats with Float and doubles with Double
  • Strings with String, binary data with Data, dates with Date, URLs with the URL type
  • Collection types with Array and Dictionary

Internally the UserDefaults class can only store NSData, NSString, NSNumber, NSDate, NSArray and NSDictionary classes.

For example, in this project, I want to save several account’s information such as email, code, name, token, and UserID. So I set values from loginResponse as the values of the keys (Email, LawyerCode, LawyerName, Token, UserID). All the values are string.

Besides account info, there’s a key named "Token" that is used for login info. If there’s value on that key, then the user is logged in, vice versa. You can also use flags, for example you can call it "isLoggedIn" with boolean values.

Getting Data in User Defaults

Getting data in User Defaults is as simple as saving it. Let’s see example below.

Account page on my project

For this page above, I need account’s name (ex: lawyer_staging) and email (ex: lawyer_staging@justika.com), which is saved in User Defaults with the key "Email" and "LawyerName" . So here’s what we’re going to do:

To show the account’s name and email you can just set the label’s text to the data you get from UserDefaults. Yes, it’s as simple as that!

Reset Data in User Defaults

Maybe you’ve been wondering, since I don’t use flags for login info, how would it be when user is not logged in?

If the user’s logged out, then we can reset the keys and values on the User Default. How do we do that?

So, when user clicks “Keluar” button, we can reset all the User Defaults’ key and values for login info. When user’s logged in, the key and the values are set again in User Defaults.

User Interface Language on User Defaults

As mentioned above, we also can save app settings like user interface language on User Defaults. For example, fonts info are saved in User Defaults.

Info.plist

On the image above, you can see that there’s a key called "Fonts provided by application" and that’s where we save the fonts.

Conclusion

We can easily use User Defaults for saving simple pieces of data such as user’s information, app settings, and flags in the form of string, boolean, integers, arrays, dictionaries, dates and more.

--

--