Capacitor Preferences for Ionic App Settings: Design and Limits
Use Capacitor Preferences for small, non-sensitive Ionic app settings. Covers trust boundaries, failure modes, and when to switch to SQLite or secure storage.
08 Mar 2026, 06:19 UTC

The problem and the practical takeaway
You are building an Ionic app with Capacitor and need to remember a user's theme choice, last screen, or feature flag after the app restarts. You do not want to operate a database for a few strings. The useful takeaway: use Capacitor Preferences for small, non-sensitive key-value strings only. Treat it as unencrypted app-local storage, not a secret vault or a structured database.
Requirements
- An Ionic Framework application using Capacitor as the native runtime.
- Targets iOS, Android, or the web through a Capacitor WebView.
- Data to persist: short strings such as UI settings, toggles, or cached identifiers.
- Data must survive app restarts and remain available without network access.
Capacitor is the official native runtime for Ionic apps. It exposes device APIs through a bridge between the web code and a native WebView. A WebView is the embedded browser engine that renders your Ionic UI inside the native app.
Smallest suitable design
Install the official preferences plugin and wrap it in a small service so components do not call the plugin directly.
npm install @capacitor/preferences
Run this command in the project root. No elevated permissions are required. The risk is a version mismatch between the plugin and the Capacitor runtime; compare the installed Capacitor version in package.json with the plugin's peer requirements before installing.
import { Preferences } from '@capacitor/preferences';
export class SettingsStore {
async set(key: string, value: string): Promise {
await Preferences.set({ key, value });
}
async get(key: string): Promise {
const ret = await Preferences.get({ key });
return ret.value ?? null;
}
async remove(key: string): Promise {
await Preferences.remove({ key });
}
}
Preferences stores strings. If you need a boolean or number, serialize it with JSON.stringify before writing and parse it after reading. Keep values small; Preferences is not intended for large blobs or lists.
After installing or updating any Capacitor plugin, run:
npx cap sync
Run this in the project root. It copies plugin code into the native iOS and Android projects. Skipping it is a common cause of silent failures or crashes in production builds because the native side does not know about the JavaScript plugin.
Trust and data boundaries
| Boundary | Behavior |
|---|---|
| JavaScript to native | Calls cross the Capacitor bridge asynchronously. A missing native plugin returns an error or undefined behavior depending on the platform. |
| Native storage | iOS writes to UserDefaults. Android writes to SharedPreferences. The web fallback writes to browser localStorage. These locations are inside the app sandbox, so other apps cannot read them under normal conditions. |
| Encryption | Preferences stores values unencrypted by default. Do not store passwords, session tokens, or personally identifiable information here. |
| Device compromise | On a rooted or jailbroken device, or through unencrypted backups, the underlying storage files may be readable outside the app. |
Operational checks
- Write a key, restart the app on a physical iOS or Android device, and read the key back. The returned value should match the value written before restart.
- Enable airplane mode and confirm previously stored values still read successfully. Preferences does not require network access.
- Inspect native storage locations if you need to verify the plugin wrote the expected pair: iOS UserDefaults or Android SharedPreferences XML. Exact file paths vary by OS version and device.
- Confirm package.json and capacitor.config.ts list compatible Capacitor and plugin versions.
Emulators and desktop browsers can behave differently from physical devices. WebView storage quotas, clearing behavior, and plugin availability differ, so physical-device testing is required before release.
Failure modes
- Running outside Capacitor: If a user opens your web app in a mobile browser instead of the Capacitor WebView, the native plugin is unavailable. Detect the environment or degrade gracefully.
- Missing npx cap sync: The JavaScript call may succeed silently while the native side has no plugin implementation, leading to crashes or lost data in production builds.
- Type coercion: Every value is stored and returned as a string. A missing key returns null, which is indistinguishable from a stored null string unless you check explicitly.
- Operating system cleanup: The OS may clear WebView storage when storage is low or when the user clears app data. Do not treat Preferences as durable long-term archive storage.
- No transactions: Multiple writes are not atomic. If the process dies between writes, you can end up with partially updated state.
Conditions that would change the design
| Condition | Suggested alternative |
|---|---|
| Storing passwords, tokens, or PII | Use platform secure storage: iOS Keychain or Android EncryptedSharedPreferences, exposed through a Capacitor community plugin. |
| Structured or relational data | Use SQLite through a Capacitor community plugin or Ionic Storage with a SQLite driver. |
| Large values or binary blobs | Use the Capacitor Filesystem plugin or native document directories. |
| Sharing data with another app from the same developer | Use iOS App Groups or Android shared user ID mechanisms, not Preferences. |
Verification and rollback
To verify the design, write a setting such as user_theme, close the app completely, reopen it, and read user_theme. If the value matches, persistence across restarts is working. Repeat on a physical device for each target platform.
Adding the plugin and running npx cap sync changes the native project state. To roll back:
npm uninstall @capacitor/preferences
npx cap sync
Run both commands in the project root. This removes the plugin code from the native projects. Any keys already written to UserDefaults or SharedPreferences remain on the device until the app is uninstalled or you call Preferences.clear() in a release build.
Limitations
- String-only values with no enforced schema.
- Unencrypted by default.
- No query language, indexing, or relations.
- Web fallback relies on browser localStorage, which users or browsers can clear independently of the app.
If your requirements move beyond short, non-sensitive strings, replace Preferences before it becomes embedded in multiple components. The wrapper service above makes that replacement easier because only the service implementation changes.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.