The Keychain
API allowing secure storage, retrieval, and management of sensitive data. This guide explains the methods and options available to use the Keychain
API effectively.
Keychain.set(key: string, value: string, options?: KeychainOptions): void
Description
Encrypts and saves the specified key
with the given value
. If the key
already exists in the Keychain, its associated value will be updated.
Parameters
key
: A unique identifier for the value to store (string)value
: The value to securely store (string)options
(optional): Configuration options for the Keychain itemUsage Example
Keychain.get(key: string, options?: KeychainOptions): string | null
Description
Decrypts and retrieves the value associated with the specified key
. Returns null
if the key
does not exist.
Parameters
key
: A unique identifier for the value to retrieve (string)options
(optional): Configuration options for accessing the Keychain itemReturns
null
if the key
is not found.Usage Example
Keychain.remove(key: string, options?: KeychainOptions): void
Description
Deletes the value associated with the specified key
. If the key
does not exist, this method does nothing.
Parameters
key
: A unique identifier for the value to delete (string)options
(optional): Configuration options for the Keychain itemUsage Example
Keychain.contains(key: string, options?: KeychainOptions): boolean
Description
Checks if the Keychain contains a value for the specified key
.
Parameters
key
: A unique identifier for the value to check (string)options
(optional): Configuration options for the Keychain itemReturns
true
if the Keychain contains the key
, otherwise false
.Usage Example
Configuration options for Keychain items.
Property | Type | Description |
---|---|---|
accessibility |
KeychainAccessibility |
Specifies when the Keychain item is accessible |
synchronizable |
boolean |
Indicates whether the Keychain item synchronizes through iCloud |
Specifies when the Keychain item is accessible.
Available values:
passcode
: Data is accessible only when the device is unlocked. Requires a passcode and does not migrate to new devices.unlocked
: Data is accessible while the device is unlocked by the user.unlocked_this_device
: Same as unlocked
, but does not migrate to new devices.first_unlock
: Data is accessible after the first unlock following a device restart.first_unlock_this_device
: Same as first_unlock
, but does not migrate to new devices.