Shift2App
Home chevron_right Local Storage
storage

Local Storage

Persistent key-value storage on device

Overview

Native storage that persists across sessions

Store data locally on the device using native SharedPreferences. Data persists even after app restart, unlike WebView localStorage which can be cleared.

check_circle
More Reliable Than localStorage

Native storage is more persistent than browser localStorage which can be cleared by the system.

setItem()

Save data

shift2appjs.setItem(key, value)

Stores a value with the specified key. Values are stored as strings.

Parameters
key String - Unique identifier for the data
value String - Data to store (use JSON.stringify for objects)
Returns

void

javascript
// Store simple value
shift2appjs.setItem("username", "john_doe");

// Store object
const user = { id: 123, name: "John", email: "john@example.com" };
shift2appjs.setItem("user_data", JSON.stringify(user));

// Store settings
shift2appjs.setItem("dark_mode", "true");
shift2appjs.setItem("notification_enabled", "true");

getItem()

Retrieve data

shift2appjs.getItem(key)

Retrieves the value stored with the specified key.

Parameters
key String - Key to look up
Returns

String - The stored value, or empty string if not found

javascript
// Get simple value
const username = shift2appjs.getItem("username");

// Get and parse object
const userData = shift2appjs.getItem("user_data");
if (userData) {
    const user = JSON.parse(userData);
    console.log("Welcome back,", user.name);
}

// Check boolean setting
const darkMode = shift2appjs.getItem("dark_mode") === "true";
if (darkMode) {
    document.body.classList.add("dark");
}

removeItem()

Delete data

shift2appjs.removeItem(key)

Removes the value stored with the specified key.

Parameters
key String - Key to remove
Returns

void

javascript
// Remove on logout
function logout() {
    shift2appjs.removeItem("user_data");
    shift2appjs.removeItem("auth_token");
    window.location.href = "/login";
}
shift2appjs.clearStorage()

Removes all stored data.

Returns

void

javascript
// Clear all data
function resetApp() {
    if (confirm("This will clear all your data. Continue?")) {
        shift2appjs.clearStorage();
        window.location.reload();
    }
}

getAllKeys()

List all stored keys

shift2appjs.getAllKeys()

Returns an array of all stored keys.

Returns

String - JSON array of keys

javascript
// List all keys
const keys = JSON.parse(shift2appjs.getAllKeys());
console.log("Stored keys:", keys);

// Example output: ["username", "user_data", "dark_mode"]

Complete Example: User Preferences

javascript
// Preferences manager
const Preferences = {
    get(key, defaultValue = null) {
        if (typeof shift2appjs === "undefined") {
            return localStorage.getItem(key) || defaultValue;
        }
        const value = shift2appjs.getItem(key);
        return value || defaultValue;
    },
    
    set(key, value) {
        if (typeof shift2appjs === "undefined") {
            localStorage.setItem(key, value);
        } else {
            shift2appjs.setItem(key, value);
        }
    },
    
    getObject(key, defaultValue = {}) {
        const json = this.get(key);
        try {
            return json ? JSON.parse(json) : defaultValue;
        } catch {
            return defaultValue;
        }
    },
    
    setObject(key, value) {
        this.set(key, JSON.stringify(value));
    }
};

// Usage
Preferences.set("theme", "dark");
Preferences.setObject("user", { id: 1, name: "John" });

const theme = Preferences.get("theme", "light");
const user = Preferences.getObject("user");
dashboard Dashboard menu_book Docs science Playground