Preferences API

Preferences are pieces of information that you store persistently and use to configure the app. Apps often expose preferences to users so that they can customize the appearance and behavior of the app based on the user’s requirement. Most preferences are stored locally by using corresponding native user defaults or storage system.

Preferences are key or value that are stored persistently and used to configure your app. The key is an arbitrary name of the preference, and the value can be a string, numbers, dates, boolean values, or data objects.

For example, you can allow users to specify their preferred units of measurement or media playback speed. Your app will store these preferences by transferring values to a set of parameters in the user’s preference record or database.

Preference values are cached so that your app doesn’t have to access the database each time when preference values are required. The Preference API is supported on Android, iOS, and Web platforms.

The following are some of the main functions of Preferences API:

  • Provide convenient methods for storing and retrieving user preferences.
  • Supports storing of preferences associated with your app only. Your app cannot access or modify the preferences of other apps
  • Helps to remove the single or all the preference values set in the preferences.

Methods


getPreference

Use this method to retrieve the value associated with one of the user’s preference key.

Syntax 

$m.getPreference(key);

Parameters

key: (String). Value to retrieve the object from the preference associated with the key.

Return

The object associated with the specified key if it is available in the database. Returns undefined, if not found.

Example

$m.getPreference("name");


putPreference

Use this method to set the value of the specified preference key.

Syntax 

$m.putPreference(key, value);

Parameters

key: (String). The key of the preference with which to associate the value.

Value: (String, Boolean, Number, Object). Object to store in the preferences database.

Example

$m.putPreference("name", "Alex");


removePreference

Use this method to remove the value of the specified preference key.

Syntax 

$m.removePreference(key);

Parameters

key: (String). The key whose value needs to be removed.

Example

$m.removePreference("name");


removeAllPreference

Use this method to remove all the key-value pairs in the preferences.

Syntax 

$m.removeAllpreference();

Parameters

None

Example

$m.removeAllPreference();