Hi Quinn
Thanks for the reply. I removed kSecAttrSynchronizable added kSecUseDataProtectionKeychain. For now, I also removed kSecAttrAccessible as part of the initial query. Here is the refactored code:
public func addItem(value: Data, forKey: String, accessControlFlags: SecAccessControlCreateFlags? = nil) {
guard !forKey.isEmpty else {
return
}
var query: [String: Any] = [kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: Bundle.main.bundleIdentifier!,
kSecAttrAccount as String: forKey,
kSecValueData as String: value,
kSecUseDataProtectionKeychain as String: true]
// Check if any access control is to be applied.
if let accessControlFlags = accessControlFlags {
var error: Unmanaged<CFError>?
guard let accessControl = SecAccessControlCreateWithFlags(kCFAllocatorDefault, kSecAttrAccessibleWhenUnlockedThisDeviceOnly, accessControlFlags, &error) else {
return
}
query[kSecAttrAccessControl as String] = accessControl
}
let status = SecItemAdd(query as CFDictionary, nil)
guard status != errSecDuplicateItem else {
return
}
guard status == errSecSuccess else {
let message = SecCopyErrorMessageString(status, nil) as String? ?? "Unknown error"
print(message)
return
}
}
And to invoke the function:
try? addItem(value: "Hello World", forKey: "greeting", accessControlFlags: [.userPresence])
The error which occurs in both the simulator and device is -25293 The user name or passphrase you entered is not correct.
Appreciate if you have any guidance on this one...
Thanks