The application-settings
module allows you to save and restore information related to your application.
Basic usage of the application-settings
module in a component:
const appSettings = require("tns-core-modules/application-settings");
function onNavigatingTo(args) {
appSettings.setBoolean("isTurnedOn", true);
appSettings.setString("username", "Wolfgang");
appSettings.setNumber("locationX", 54.321);
const isTurnedOn = appSettings.getBoolean("isTurnedOn");
const username = appSettings.getString("username");
const locationX = appSettings.getNumber("locationX");
// Will return "No string value" if there is no value for "noSuchKey"
const someKey = appSettings.getString("noSuchKey", "No string value");
// Will return false if there is no key with name "noSuchKey"
const isKeExisting = appSettings.hasKey("noSuchKey");
}
exports.onNavigatingTo = onNavigatingTo;
function onClear() {
// Removing a single entry via its key name
appSettings.remove("isTurnedOn");
// Clearing the whole application-settings for this app
appSettings.clear();
}
import {
getBoolean,
setBoolean,
getNumber,
setNumber,
getString,
setString,
hasKey,
remove,
clear
} from "tns-core-modules/application-settings";
export function onNavigatingTo(args) {
setBoolean("isTurnedOn", true);
setString("username", "Wolfgang");
setNumber("locationX", 54.321);
const isTurnedOn: boolean = getBoolean("isTurnedOn");
const username: string = getString("username");
const locationX: number = getNumber("locationX");
// Will return "No string value" if there is no value for "noSuchKey"
const someKey: string = getString("noSuchKey", "No string value");
// Will return false if there is no key with name "noSuchKey"
let isKeExisting: boolean = hasKey("noSuchKey");
}
function onClear() {
// Removing a single entry via its key name
remove("isTurnedOn");
// Clearing the whole application-settings for this app
clear();
}
Improve this document
Demo Source
Name |
Type |
Description |
clear |
void |
Removes all stored values. |
flush |
boolean |
Flush all changes to disk synchronously. The return flag indicates if changes were saved successfully to disk. |
getAllKeys |
Array<string> |
Array containing all stored keys |
getBoolean(key: string, deafaultValue?: boolean) |
boolean |
Gets a value (if existing) for a key as a Boolean Object. A default value can be provided in case there is no existing value. |
getNumber(key: string, deafaultValue?: number) |
number |
Gets a value (if existing) for a key as a Number Object. A default value can be provided in case there is no existing value |
getString(key: string, deafaultValue?: string) |
string |
Gets a value (if existing) for a key as a String Object. A default value can be provided in case there is no existing value. |
hasKey(key: string) |
boolean |
Checks whether such a key exists. |
remove |
void |
Removes an entry by its key name. |
setBoolean(key: string, value: boolean) |
void |
Sets a Boolean Object for a key. |
setNumber(key: string, value: number) |
void |
Sets a Number Object for a key. |
setString(key: string, value: string) |
void |
Sets a String Object for a key. |