NOTE! You are browsing legacy documentation. For latest visit docs.nativescript.org.

NativeScript Angular

Application Settings

The application-settings module allows you to save and restore information related to your application.

Usage

Basic usage of the application-settings module in a component:

import { Component } from "@angular/core";
import {
    getBoolean,
    setBoolean,
    getNumber,
    setNumber,
    getString,
    setString,
    hasKey,
    remove,
    clear
} from "tns-core-modules/application-settings";

@Component({
    moduleId: module.id,
    templateUrl: "./usage.component.html"
})
export class UsageComponent {

    constructor() {
        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");
    }

    onClearß() {
        // Removing a single entry via its key name
        remove("isTurnedOn");

        // Clearing the whole application-settings for this app
        clear();
    }
}

Methods

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.

API References

Name Type
tns-core-modules/application-settings Module

Native Component

Android iOS
android.content.SharedPreferences NSUserDefaults