Apps and programs need to set settings or preferences that can be retrieved anytime to customize app look and behavior. In this article, we will look at how to do it in Android as well as iOS. We can store various types of user settings like integers, floats, bool, string, dictionaries, arrays etc. Avoid storing too many settings as it may slow down app launch.
For Android, we will use Kotlin and for iOS, we will use Swift.
Settings/Preferences in Android
Preferences in android can be set as follows. Following snippet shows how to save string preference. In android, we can set values of types String, Integer, Boolean, Float, Long and String Set.
1 2 3 4 5 6 7 8 9 |
val sharedPref: SharedPreferences = activity.getPreferences(Context.MODE_PRIVATE) val editor = sharedPref.edit() editor.putString(“userName”, “Amitabh”) editor.putInt(“Age”,66) editor.putBoolean(“Working”, true) editor.putFloat(“Score”,5.6f) editor.putLong(“time”,5555566666666) editor.putStringSet(“children”,setOf(“Rakesh”, “Lalita”, “Pankaj”)) editor.apply() |
Reading preferences in Android
Preferences can be read back as follows. We need to provide default return values in case preference with that key is not set.
1 2 3 4 5 6 7 |
val sharedPref = activity.getPreferences(Context.MODE_PRIVATE) var age = sharedPref.getInt(“age”, 0) var name = sharedPref.getString(“name”,“No name”) var working = sharedPref.getBoolean(“working”,false) var score = sharedPref.getFloat(“score”,0f) var time = sharedPref.getLong(“time”,0) var children = sharedPref.getStringSet(“children”, setOf(“”)) |
Settings/Preferences in iOS
Settings or preferences are also called User Defaults in iOS. First we have create defaults object and then use it as follows.
1 2 3 4 5 6 7 |
let defaults = UserDefaults.standard defaults.set(28, forKey: “Age”) defaults.set(true, forKey: “UseTouchID”) defaults.set(9.81, forKey: “gravity”) defaults.set(“Jitendra Surve”, forKey: “Name”) defaults.set(Date.now, forKey: “LastRun”) |
We can also set arrays and dictionaries as iOS app user defaults.
1 2 3 4 5 |
let array = [“Hi”, “Sweatheart”] defaults.set(array, forKey: “SavedArray”) let dict = [“Name”: “Leone”, “Country”: “US”] defaults.set(dict, forKey: “SavedDict”) |
Reading userDefaults in iOS
User defaults can be used using key as shown below by storing them in a variable.
1 2 3 |
let age = defaults.integer(forKey: “Age”) let useTouchID = defaults.bool(forKey: “UseTouchID”) let gravity = defaults.double(forKey: “gravity”) |
Remember that when reading back user defaults, system may return a default value if that userDefault is not set. Default values as as per type of userDefault.
- integer(forKey:) returns an integer if the key existed, or 0 if not.
- bool(forKey:) returns a boolean if the key existed, or false if not.
- float(forKey:) returns a float if the key existed, or 0.0 if not.
- double(forKey:) returns a double if the key existed, or 0.0 if not.
- object(forKey:) returns AnyObject? so you need to conditionally typecast it to your data type.
When retrieving objects, the result is optional. This means you can either accept the optionality, or typecast it to a non-optional type and use the nil coalescing operator to handle missing values. For example:
1 |
let savedArray = defaults.object(forKey: “SavedArray”) as? [String] ?? [String]() |