Keychain Access Control
Gate items behind biometrics and device unlock.
Controlling When Secrets Are Readable
Storing a secret is only half the job — you must control when it can be read. The Keychain offers accessibility classes and access-control flags that gate items behind device unlock, passcode, or biometrics. This protects data if a device is lost.
import Security
// Two layers of control:
// kSecAttrAccessible -> when readable (lock state)
// SecAccessControl -> user presence / biometricsThe kSecAttrAccessible Key
kSecAttrAccessible sets the accessibility class on add. It decides whether an item is available always, only after first unlock, or only while the device is unlocked. Pick the strictest level your feature allows.
import Security
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrAccount as String: "user",
kSecValueData as String: Data("secret".utf8),
kSecAttrAccessible as String:
kSecAttrAccessibleWhenUnlocked
]
_ = queryAll lessons in this course
- Storing Secrets in the Keychain
- Keychain Access Control
- Biometric Authentication
- Data Protection and Encryption