Hi
I'm trying to add to the Keychain with access control flags. However the OSStatus
returns -50 One or more parameters passed to a function were not valid.
Here is the function I've written causing the error:
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,
kSecAttrSynchronizable as String: false
kSecAttrAccessible as String: kSecAttrAccessibleAfterFirstUnlock]
// 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
}
}
Any ideas why this might be occurring, if kSecAttrAccessControl
is not added to the query
parameter, then it works fine.
Thanks