Explore tens of thousands of sets crafted by our community.
iOS Keychain Services
15
Flashcards
0/15
Adding an Item
To add an item, prepare a `NSDictionary` of attributes and pass it to `SecItemAdd`. Example: `let query = [kSecClass: kSecClassGenericPassword, kSecAttrAccount: "User"]`
Querying Items
To find items, create a search query and use `SecItemCopyMatching`. Example: `SecItemCopyMatching(query as CFDictionary, &item)`
Keychain Basics
Keychain services provide secure storage for passwords, certificates, and other secrets. Example: Using `SecItemAdd` to add an item to the keychain.
Updating Items
Update existing items by specifying attributes to change using `SecItemUpdate`. Example: `SecItemUpdate(query as CFDictionary, attributesToUpdate as CFDictionary)`
Deleting Items
Items can be deleted using `SecItemDelete` and a query. Example: `SecItemDelete(query as CFDictionary)`
Access Control
Define how a keychain item is accessed, by using `SecAccessControlCreateWithFlags`. Example: `let access = SecAccessControlCreateWithFlags(nil, kSecAttrAccessibleWhenUnlocked, .userPresence, &error)`
Keychain Error Handling
Handle errors from keychain operations by checking the returned status. Example: `let status = SecItemAdd(query as CFDictionary, nil); if status != errSecSuccess { handle error }`
Synchronizing Keychain Items
Keychain items can be synchronized across devices using iCloud by setting `kSecAttrSynchronizable`. Example: `query[kSecAttrSynchronizable] = kSecAttrSynchronizableAny`
Securing with Touch ID/Face ID
Secure items with biometrics by setting `kSecAccessControlUserPresence` for an access control object. Example: `let accessControl = SecAccessControlCreateWithFlags(nil, kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly, .userPresence, nil)`
Sharing Keychain Items
Items can be shared between apps by using `kSecAttrAccessGroup`. Example: `query[kSecAttrAccessGroup] = "<App Identifier Prefix>.com.example.shared"`
Accessing Internet Passwords
For passwords related to internet accounts, use `kSecClassInternetPassword`. Example: `let query = [kSecClass: kSecClassInternetPassword, kSecAttrServer: "www.example.com"]`
Concurrency with Keychain
iOS Keychain API is thread-safe, allowing for concurrent access. Example: Use GCD or `OperationQueue` to access the keychain concurrently without issues.
Persistent References
Use persistent references to a keychain item to avoid repeatedly querying for it. Example: `let query: [String: Any] = [kSecClass: kSecClassGenericPassword, kSecReturnPersistentRef: true]`
Retrieving Passwords
To get a password from the keychain, set `kSecReturnData` to get data along with the item associated with `kSecClassGenericPassword`. Example: `query[kSecReturnData] = kCFBooleanTrue`
Keychain Item Attributes
Keychain items can have various attributes like `kSecAttrService`, `kSecAttrAccount`. Example: `let attributes = [kSecAttrService: "ExampleService", kSecAttrAccount: "ExampleAccount"]`
© Hypatia.Tech. 2024 All rights reserved.