Settings API provides access to iOS app settings stored in NSUserDefaults. It allows reading and writing settings values and observing changes to specific settings keys.
Import
Methods
get()
key: The settings key to retrieve
undefined if not found.
Example
set()
settings: An object containing key-value pairs to store
The
set() method updates the local cache and persists values to NSUserDefaults.Example
watchKeys()
keys: A single key string or array of key strings to watchcallback: Function called when any watched key changes
clearWatch() to unsubscribe.
Example
clearWatch()
watchId: The watch ID returned bywatchKeys()
Example
Usage Examples
Basic Settings Management
Watching Settings Changes
Multiple Settings
User Preferences Hook
Persisting App State
Notes
- Settings only works on iOS and uses
NSUserDefaultsinternally. - Values are automatically persisted across app launches.
- Supported value types include strings, numbers, booleans, and objects (serializable to JSON).
- Changes made via
set()trigger callbacks registered withwatchKeys(). - The callback receives no arguments; use
get()to retrieve updated values. - Watch IDs are integers starting from 0.
- It’s important to call
clearWatch()in cleanup functions to prevent memory leaks. - For cross-platform settings storage, consider using AsyncStorage or a third-party solution.
- Settings are app-specific and not shared between apps.