User preferences

Hi there,

I’m coming from Xcode Developpement. In Xcode there is an easy way to save user preferences in a dictionary (Key, Value). Key is a string and value a basic string/integer/bool.

In Xojo how can we manage user preferences in macOS and iOS application ?

Check out macOS lib at https://github.com/macoslib/macoslib. The library should contain the preferences declares. Otherwise, use the MBS plugin which you will need anyways if you don’t want to waste time. See https://www.monkeybreadsoftware.net/class-cfpreferencesmbs.shtml.

1 Like

We have an example, which uses a SQLite database and so works cross platform:

and you just use it with Key/Value like this:

Sub Open()
pref.init

dim n as integer = pref.value("Counter")

MsgBox str(n)+"nth launch."
pref.Value("Counter") = n +1
End
2 Likes

@Christian_Schmitz by cross platform did you mean this work on macOS, iOS and linux app ?

Yes, all. Including Windows.

Save the user’s prefs in an SQLite database.

@TimStreater, that is what the referenced example above does.

You can create a dictionary and store your values in there. The dictionary can be saved as a JSON text file.

When your app opens, you can load this data in a dictionary in your app, to access it whenever you need. When your app closes, or whenever you change this dictionary, you could save the file. Remember that any string data needs to have be UTF8 encoded.

Xojo documentation:
Working with dictionary and JSON
Saving the JSON data to a file
Loading the JSON data from a file
Write / Save using a Binary Stream also works

Make sure it is UTF8
Define Encoding

When you store string data in a dictionary, you should make sure it is UTF8, as I mentioned:

Var d As New Xojo.Core.Dictionary
d.value( SomeKeyValue ) = MyStringValue.DefineEncoding( Encodings.UTF8 )

By the way, the if the key value is a string, this needs to be UTF8 as well.

Thank you @Edwin_van_den_Akker for your help. I will implement your solution and save data in the special app folder. :open_hands:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.