Store variables to APP local.

iOS

Use UserDefaults to load & save key-value pairs in APP local.

I prefer to create a singleton Settings class to store all the local variables and load the values of these variables when the singleton class is initialized. Typically, I initialize the Settings class when the app starts.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Settings {

static let shared: Settings = {
let shared = Settings()
return shared
}()

var userID = ""
var userPwd = ""
var devID = 0
var enableDev = false

private init(){
print("Setting init")
self.load()
}

func load(){
self.userID = UserDefaults.standard.value(forKey: "uid") as? String ?? ""
self.userPwd = UserDefaults.standard.value(forKey: "pwd") as? String ?? ""
self.devID = UserDefaults.standard.value(forKey: "did") as? Int ?? 0
self.enableDev = UserDefaults.standard.value(forKey: "enableDev") as? Bool ?? false

print("userID=\(self.userID) userPwd=\(self.userPwd) devID=\(self.devID), enableDev=\(self.enableDev)")
}

func save(){
UserDefaults.standard.set(self.userID, forKey: "uid")
UserDefaults.standard.set(self.userPwd, forKey: "pwd")
UserDefaults.standard.set(self.devID, forKey: "did")
UserDefaults.standard.set(self.enableDev, forKey: "enableDev")
}
}
1
2
3
4
5
6
7
//Setting & saving the variables
Setting.shared.userID = "test123"
Setting.shared.userPwd = "12345678"
Setting.shared.devID = 1980
Setting.shared.enableDev = true

Setting.shared.save()

Android

Use SharedPreferences to load & save key-value pairs in APP local.

I prefer to create an application class to store all the local variables and initialize the values of these variables when the app starts.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class MyApplication: Application() {

companion object{
var userID = ""
var userPwd = ""
var devID = 0
var enableDev = false

var preferences: SharedPreferences? = null
fun save(){
preferences?.let {
it.edit()
.putString("uid", MyApplication.userID)
.putString("pwd", MyApplication.userPwd)
.putInt("did", MyApplication.devID)
.putBoolean("enableDev", MyApplication.enableDev)
.apply()
}
}
}

override fun onCreate(){
super.onCreate()
preferences = getSharedPreferences( packageName + "_preferences", MODE_PRIVATE);
load()
}

private fun load(){
preferences?.let {
userID = it.getString("uid", "")?:""
userPwd = it.getString("pwd", "")?:""
devID = it.getInt("did", 0)
enableDev = it.getBoolean("enableDev", false)
}
println("userID=$userID userPwd=$userPwd devID=$devID, enableDev=$enableDev")
}
}
1
2
3
4
5
6
7
8
9
10
11
//Do remember to add the new application class to the AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxx.xxx">

<application
android:name=".MyApplication"
...
</application>

</manifest>
1
2
3
4
5
6
7
//Setting & saving the variables
MyApplication.userID = "test123"
MyApplication.userPwd = "12345678"
MyApplication.devID = 1980
MyApplication.enableDev = true

MyApplication.save()