Major update for stability
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user