I have implemented the following code to create a keychain item:
let keychainQuery = NSMutableDictionary(dictionary: [
NSString(format: kSecClass) : kSecClassGenericPasswordValue,
NSString(format: kSecAttrService) : "my_service",
NSString(format: kSecAttrAccount) : "my.app.domain",
NSString(format: kSecAttrAccessible) : NSString(format: kSecAttrAccessibleAfterFirstUnlock),
NSString(format: kSecReturnData) : "my_password")
let status = SecItemAdd(keychainQuery as CFDictionary, nil)
if status != errSecSuccess (
print("status not success: \(status)")
}
Unfortunately, when the iPhone is locked (press the lock button), even though the accessible element has been set to "after first unlock", I get an error (-25300) when my code tries to retrieve the information stored in the keychain. The app can access information in the keychain while the app is in the background, just not when the iPhone is locked.
The code to retrieve:
let keychainQuery: NSMutableDictionary = NSMutableDictionary(dictionary: [
kSecClassValue: kSecClassGenericPasswordValue,
NSString(format: kSecAttrService) : "my_service",
NSString(format: kSecAttrAccount) : "my.app.domain",
NSString(format: kSecReturnData) : kCFBooleanTrue,
NSString(format: kSecMatchLimit) : NSString(format: kSecMatchLimitOne)
])
iOS 10.3.1
Is there any way to allow programmatic access to contents in the keychain on iOS when an app is in the background and the phone is locked?
Thanks,
Karl