You’ll often want default settings that users can change. iOS provides the settings bundle for storing this information. Let’s look at how to use the settings bundle.
Download the starter example file. I created a project which eventually will display the user’s default pizza. We’ll finish the code to add that pizza.
To add a settings menu Go to File>New>File on the menu. In the filter type sett. Under resources you’ll see the settings bundle. You’d Select it and click Next, then Save it into the project group. I’ve already set one up for you, so you can hit Cancel.
Find the settings bundle, click the arrow to find the root.plist. The third item is Preference Items. Open that.
You’ll find several items already here. There are several types of controls here. Item 3 is sliders. I find them difficult to use in settings. Delete the slider by clicking to the left of the arrow to select and hit Delete on your keyboard. Right-click on Item 1. A drop down asks you for what control here and select multi value.
Expand out all the Items. Each has different values. The Type is the type of control, the Title is the title showing in the settings app, and Identifier is your key for accessing this value. I set up the text field and the toggle switch. Let set up together the Multi-value.
For title make it Size. For Identifier, make it size_preference. I’m going to leave the default value blank, but click on default value to get the + icon. Click the icon and you’ll see two more attributes not here yet. Titles and Values. You need both for a multi value. Click on Titles first. In title, hit the plus four times. For item names add Personal, Small, Medium and Large. Go back to default value, and hit the plus again to add the values, which pair with the titles. Add one value and change the type to number. Add three more items.
You’ve now added the multi value. Head to ViewController.swift I’ve added some code here for you, but we’ll now complete it.
Userdefaults.standard is a singleton for the user defaults settings. I’ll make a shorter identifier for
let defaults = UserDefaults.standard
User defaults has several methods with the key as the identifier from the Root.plist. These methods, based on type retrieves the data from the settings page. The switch uses a
Bool, and the text uses an optional string.
Let’s add the MutiValue. We made the value an integer, so we can use integer here, then add it to the string.
let size = defaults.integer(forKey: "size_preference") text += String(format: "%i\" ", size)
Head down to
We need to tell the app we have a settings bundle. Add the following code to register the settings bundle with a dictionary with a string key.
While the code we have will work, if a user changes default settings while using the app, that will not show up in the app. We need a key-value observer to tell us when there is a change, and to update our favorite pizza.
NotificationCenter.default.addObserver(self,selector: #selector(defaultsChanged),name: UserDefaults.didChangeNotification,object: nil)
For the selector, I set the
defaultsChanged method to an
IBAction, so I can use that.
I have a call here for
defaultsChanged already. So I’ll set the simulator to iPhone 8 plus and run. We get a boring pizza. Head over to settings and scroll to the bottom to find ad select the favorite pizza settings. I’ll add some topping, make a pan pizza and make the pizza medium. Go back to the app and you find your pizza there.
That’s all you need to get settings into your app for defaults and other important user preferences that don’t change often. For more on the attributes, check out the pdf chart I placed in the assets folder of the exercise file. There’s a lot you can do.