iOS stores private information such as passwords, encryption keys, certificates, and identities in a secure storage area called the Keychain. This storage area is managed completely by a co-processor called the Secure Enclave, which is embedded inside the application processor. Because the Keychain is sandboxed on iOS, keychain items may only be retrieved by the application that put them there in the first place.
In some cases you must turn on Keychain Sharing in Xcode capabilities in order to avoid errors.
In order to interact with the keychain, we use a c framework called Keychain Services. For more information, see Apple's Keychain Services Programming Guide.
Because Keychain Services is below the
Foundation level, it is restricted to using
CoreFoundation types. As a result, most objects are internally represented as
CFStrings as their keys and a variety of
CoreFoundation types as their values.
While Keychain Services is included as a part of the
Security framework, importing
Foundation is usually a good option since it includes some helper functions in the backend.
Additionally, if you don't want to deal with Keychain Services directly, Apple provides the Generic Keychain Swift sample project that provides Swift types that use Keychain Services behind the scenes.