Add settings UI for your app

in ios •  6 years ago  (edited)

When to use and not to use settings UI

You want default constant values, but also want that these defaults to be changed by the app users because this constant depends on user or user locale. The values you put in this section is however something that you do not anticipate the app users to change too many times throughout the lifespan of the app. Examples can be that you want the user to be able so switch between Celcius or Fahrenheit degrees for temperature.
Regular constants like Pi, degrees in a circle (360) and the likes, still belong in your code, because they do not change and you do not whant your beloved app user to be able to change these.

Adding a settings page to your app

To get started is actually really simple.
In Xcode, click File>New>File…>
In the iOS category, select Settings Bundle and click Next button

The new settings.bundle is added to your project


If you now run your app on your iPhone device you can already have a look at this settings UI. The UI is non responsive, but it contains three elements to get you started.
The group contains a text field called Name, a toggle switch called Enabled and a slider.

The contents of the settings UI is specified in the file Root.plist and this is shown in image below.

The Identifier contains the name by which you will access the values. The text field “Name” can as you see be accessed using the identifier name_preference

So how do you read these values? Read on!

Introducing the UserDefaults object

Known as NSUserDaults in Objective C and earlier versions of Swift, the object UserDefaults lets you access the values you have set up. In this example we are not doing any changes. We are simply accessing the default text field Name

The following code snippet shows how a text value is read from the name_preference text field. If no value has been set, the string(forKey:) will return an Optional=nil and no update is done to theValueTheAppNeeds. It will stay = “The Setting”

Run the app from Xcode. When it has completed loading, click the Home button.
Open iPhone Settings>Your app name
In the settings page, set the text field value to your liking. In this example I set it = “The value of text field name”

I then stopped the code in Xcode and ran it again.
When running the code a second time, the following is printed in console

The app has successfully read the text field from your app settings page.

Note that the UserDefault values are only read when the app is first loaded. This means that changes to settings while the app is running may not be sensed by the app. If this is the case in your app, you can either do as I did in this tutorial, relaunch the app after setting the value, or you can add Notification awareness to your app.

Until next time

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Congratulations @andrebru! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!