Major update for stability

This commit is contained in:
Mark Gregory
2026-09-08 15:22:19 +01:00
parent 3b95f53a25
commit b2f3c5aef6
24 changed files with 1059 additions and 212 deletions
+26 -12
View File
@@ -19,23 +19,37 @@ enum KeychainStore {
return value
}
static func set(_ value: String, for account: String) {
@discardableResult
static func set(_ value: String, for account: String) -> OSStatus {
let base: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: service,
kSecAttrAccount as String: account
]
if value.isEmpty {
SecItemDelete(base as CFDictionary)
return
}
if value.isEmpty { return delete(account) }
let data = Data(value.utf8)
let update: [String: Any] = [kSecValueData as String: data]
if SecItemUpdate(base as CFDictionary, update as CFDictionary) == errSecItemNotFound {
var add = base
add[kSecValueData as String] = data
add[kSecAttrAccessible as String] = kSecAttrAccessibleAfterFirstUnlock
SecItemAdd(add as CFDictionary, nil)
}
let update: [String: Any] = [
kSecValueData as String: data,
kSecAttrAccessible as String: kSecAttrAccessibleWhenUnlocked
]
let updateStatus = SecItemUpdate(base as CFDictionary, update as CFDictionary)
if updateStatus != errSecItemNotFound { return updateStatus }
var add = base
add[kSecValueData as String] = data
add[kSecAttrAccessible as String] = kSecAttrAccessibleWhenUnlocked
return SecItemAdd(add as CFDictionary, nil)
}
@discardableResult
static func delete(_ account: String) -> OSStatus {
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: service,
kSecAttrAccount as String: account
]
let status = SecItemDelete(query as CFDictionary)
return status == errSecItemNotFound ? errSecSuccess : status
}
}