42 lines
1.4 KiB
Swift
42 lines
1.4 KiB
Swift
// KeychainHelper.swift
|
|
// Helper for secure storage
|
|
|
|
import Foundation
|
|
import Security
|
|
|
|
class KeychainHelper {
|
|
static let shared = KeychainHelper()
|
|
|
|
private init() {}
|
|
|
|
func save(_ value: String, forKey key: String) -> Bool {
|
|
guard let data = value.data(using: .utf8) else { return false }
|
|
|
|
let query: [String: Any] = [
|
|
kSecClass as String: kSecClassGenericPassword,
|
|
kSecAttrAccount as String: key,
|
|
kSecValueData as String: data,
|
|
kSecAttrService as String: "owen.notetaker" // Replace with your bundle ID
|
|
]
|
|
|
|
SecItemDelete(query as CFDictionary) // Delete if exists
|
|
let status = SecItemAdd(query as CFDictionary, nil)
|
|
return status == errSecSuccess
|
|
}
|
|
|
|
func get(forKey key: String) -> String? {
|
|
let query: [String: Any] = [
|
|
kSecClass as String: kSecClassGenericPassword,
|
|
kSecAttrAccount as String: key,
|
|
kSecAttrService as String: "owen.notetaker",
|
|
kSecMatchLimit as String: kSecMatchLimitOne,
|
|
kSecReturnData as String: true
|
|
]
|
|
|
|
var item: AnyObject?
|
|
let status = SecItemCopyMatching(query as CFDictionary, &item)
|
|
|
|
guard status == errSecSuccess, let data = item as? Data else { return nil }
|
|
return String(data: data, encoding: .utf8)
|
|
}
|
|
} |