Я не могу писать / читать из Keychain в приложении Mac-Catalyst на Mac, он возвращает ошибки 34018 и 25300 соответственно. Есть ли способ заставить Keychain работать на Mac в приложении Catalyst?
Xcode: 11.0, MacOS: 10.15
Вот пример кода, он работает на iOS, но не на Mac. Код печатает «Моя скрытная пчела ?», чтобы указать, что мы успешно записали этот текст в связку ключей, а затем прочитали его.
override func viewDidLoad() {
super.viewDidLoad()
let itemKey = "My key"
let itemValue = "My secretive bee ?"
deleteFromKeychain(itemKey: itemKey)
addToKeychain(itemKey: itemKey, itemValue: itemValue)
readFromKeychain(itemKey: itemKey)
}
func deleteFromKeychain(itemKey: String) {
let queryDelete: [String: AnyObject] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrAccount as String: itemKey as AnyObject,
]
let resultCodeDelete = SecItemDelete(queryDelete as CFDictionary)
if resultCodeDelete != noErr {
print("Error deleting from Keychain: \(resultCodeDelete)")
}
}
func addToKeychain(itemKey: String, itemValue: String) {
guard let valueData = itemValue.data(using: String.Encoding.utf8) else {
print("Error saving text to Keychain")
return
}
let queryAdd: [String: AnyObject] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrAccount as String: itemKey as AnyObject,
kSecValueData as String: valueData as AnyObject,
kSecAttrAccessible as String: kSecAttrAccessibleWhenUnlocked,
]
let resultCode = SecItemAdd(queryAdd as CFDictionary, nil)
if resultCode != noErr {
print("Error saving to Keychain: \(resultCode)")
}
}
func readFromKeychain(itemKey: String) {
let queryLoad: [String: AnyObject] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrAccount as String: itemKey as AnyObject,
kSecReturnData as String: kCFBooleanTrue,
kSecMatchLimit as String: kSecMatchLimitOne,
]
var result: AnyObject?
let resultCodeLoad = withUnsafeMutablePointer(to: &result) {
SecItemCopyMatching(queryLoad as CFDictionary, UnsafeMutablePointer($0))
}
if resultCodeLoad == noErr {
if let result = result as? Data,
let keyValue = NSString(data: result,
encoding: String.Encoding.utf8.rawValue) as? String {
// Found successfully
print(keyValue)
}
} else {
print("Error loading from Keychain: \(resultCodeLoad)")
}
}