@napacitor/preferences
Key-value persistent storage plugin (corresponds to @capacitor/preferences).
Installation
To use npm:
npm install @napacitor/preferences
To use yarn:
yarn add @napacitor/preferences
Sync native files:
npx nap sync
Usage
import { Preferences } from '@napacitor/preferences';
// Optional: configure a storage group (subsequent reads/writes/clears target this group; omitted = default storage)
await Preferences.configure({ group: 'session' });
await Preferences.set({ key: 'name', value: 'Max' });
const { value } = await Preferences.get({ key: 'name' }); // 'Max'
await Preferences.remove({ key: 'name' });
await Preferences.clear();
// Capacitor 2 Storage compatibility (Napacitor has no migration source: migrate returns empty results, removeOld is a no-op)
const { migrated, existing } = await Preferences.migrate(); // { migrated: [], existing: [] }
await Preferences.removeOld();
How It Works
Wraps the APIs described in the .NET MAUI Preferences documentation: Preferences.Default.Get / Set / Remove / Clear (iOS: UserDefaults / Android: SharedPreferences / Windows: ApplicationData). configure({group}) maps to MAUI's sharedName (the Android counterpart is a suite; cross-app sharing has native semantics only on Android) and applies to all subsequent reads/writes/clears; on web it maps to a localStorage key prefix (default napacitor.). MAUI Preferences does not provide key enumeration, so this plugin does not provide keys() (maintain a key list yourself if needed).
Permissions
Android
No additional configuration required.
iOS
No additional configuration required.
Windows
No additional configuration required.
API
configure(options)
Configures the storage group at runtime (subsequent reads/writes/clears target this group).
| Param | Type |
|---|---|
options | ConfigureOptions |
Returns: Promise<void>
get(options)
Reads a value by key.
| Param | Type |
|---|---|
options | GetOptions |
Returns: Promise<GetResult>
set(options)
Writes a value by key.
| Param | Type |
|---|---|
options | SetOptions |
Returns: Promise<void>
remove(options)
Removes a key.
| Param | Type |
|---|---|
options | RemoveOptions |
Returns: Promise<void>
clear()
Clears the current storage group (note: maps directly to Preferences.Clear(sharedName), clearing the whole storage group including data not written by this plugin).
Returns: Promise<void>
migrate()
Migrates from Capacitor 2 Storage. Napacitor has no migration source, so it always returns empty results.
Returns: Promise<MigrateResult>
removeOld()
Removes old Capacitor 2 Storage data (Napacitor has no migration source, no-op).
Returns: Promise<void>
Interfaces
ConfigureOptions
| Prop | Type | Description |
|---|---|---|
group | string | Storage group name (maps to MAUI Preferences' sharedName); default storage when omitted. |
GetOptions
| Prop | Type | Description |
|---|---|---|
key | string | Key to read (must not be empty, otherwise INVALID_ARGUMENTS is thrown). |
GetResult
| Prop | Type | Description |
|---|---|---|
value | string | null | Value of the key; null when never set or already removed. |
SetOptions
| Prop | Type | Description |
|---|---|---|
key | string | Key to write (must not be empty, otherwise INVALID_ARGUMENTS is thrown). |
value | string | Value to write (strings only; JSON.stringify complex objects yourself). |
RemoveOptions
| Prop | Type | Description |
|---|---|---|
key | string | Key to remove (must not be empty, otherwise INVALID_ARGUMENTS is thrown). |
MigrateResult
| Prop | Type | Description |
|---|---|---|
migrated | string[] | Migrated keys. Napacitor has no Capacitor 2 Storage migration source, always empty. |
existing | string[] | Keys that already existed (same as above, always empty). |