
Explore tens of thousands of sets crafted by our community.
iOS Keychain Services
15
Flashcards
0/15




Access Control




Define how a keychain item is accessed, by using `SecAccessControlCreateWithFlags`. Example: `let access = SecAccessControlCreateWithFlags(nil, kSecAttrAccessibleWhenUnlocked, .userPresence, &error)`




Updating Items




Update existing items by specifying attributes to change using `SecItemUpdate`. Example: `SecItemUpdate(query as CFDictionary, attributesToUpdate as CFDictionary)`




Keychain Basics




Keychain services provide secure storage for passwords, certificates, and other secrets. Example: Using `SecItemAdd` to add an item to the keychain.




Deleting Items




Items can be deleted using `SecItemDelete` and a query. Example: `SecItemDelete(query as CFDictionary)`




Querying Items




To find items, create a search query and use `SecItemCopyMatching`. Example: `SecItemCopyMatching(query as CFDictionary, &item)`




Adding an Item




To add an item, prepare a `NSDictionary` of attributes and pass it to `SecItemAdd`. Example: `let query = [kSecClass: kSecClassGenericPassword, kSecAttrAccount: "User"]`




Keychain Item Attributes




Keychain items can have various attributes like `kSecAttrService`, `kSecAttrAccount`. Example: `let attributes = [kSecAttrService: "ExampleService", kSecAttrAccount: "ExampleAccount"]`




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 }`




Sharing Keychain Items




Items can be shared between apps by using `kSecAttrAccessGroup`. Example: `query[kSecAttrAccessGroup] = "<App Identifier Prefix>.com.example.shared"`




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)`




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.




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`




Persistent References




Use persistent references to a keychain item to avoid repeatedly querying for it. Example: `let query: [String: Any] = [kSecClass: kSecClassGenericPassword, kSecReturnPersistentRef: true]`




Accessing Internet Passwords




For passwords related to internet accounts, use `kSecClassInternetPassword`. Example: `let query = [kSecClass: kSecClassInternetPassword, kSecAttrServer: "www.example.com"]`




Synchronizing Keychain Items




Keychain items can be synchronized across devices using iCloud by setting `kSecAttrSynchronizable`. Example: `query[kSecAttrSynchronizable] = kSecAttrSynchronizableAny`
© Hypatia.Tech. 2024 All rights reserved.