From e76b37222501a200b060c3462b37b374ca17456c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kevin=20Varga=20Hala=CC=81sz?= Date: Sun, 28 Mar 2021 13:46:44 +0200 Subject: [PATCH] Codable saving functionality --- SwiftKeychainWrapper/KeychainWrapper.swift | 24 +++++++++++++++++++ .../KeychainWrapperSubscript.swift | 15 ++++++++++++ 2 files changed, 39 insertions(+) diff --git a/SwiftKeychainWrapper/KeychainWrapper.swift b/SwiftKeychainWrapper/KeychainWrapper.swift index 7254086..c03e6ad 100644 --- a/SwiftKeychainWrapper/KeychainWrapper.swift +++ b/SwiftKeychainWrapper/KeychainWrapper.swift @@ -207,6 +207,17 @@ open class KeychainWrapper { return NSKeyedUnarchiver.unarchiveObject(with: keychainData) as? NSCoding } + /// Returns a Codable object for a specified key. + /// + /// - parameter forKey: The key to lookup data for. + /// - parameter withAccessibility: Optional accessibility to use when retrieving the keychain item. + /// - parameter isSynchronizable: A bool that describes if the item should be synchronizable, to be synched with the iCloud. If none is provided, will default to false + /// - returns: The Codable object associated with the key if it exists. If no codable exists, returns nil. + open func codable(forKey key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil, isSynchronizable: Bool = false) -> T? { + guard let data = data(forKey: key, withAccessibility: accessibility, isSynchronizable: isSynchronizable) else { return nil } + + return try? JSONDecoder().decode(T.self, from: data) + } /// Returns a Data object for a specified key. /// @@ -298,6 +309,19 @@ open class KeychainWrapper { return set(data, forKey: key, withAccessibility: accessibility, isSynchronizable: isSynchronizable) } + + /// Save a Codable object as data to the keychain associated with a specified key. If data already exists for the given key, the data will be overwritten with the new value. + /// + /// - parameter value: The Codable object to save. + /// - parameter forKey: The key to save the object under. + /// - parameter withAccessibility: Optional accessibility to use when setting the keychain item. + /// - parameter isSynchronizable: A bool that describes if the item should be synchronizable, to be synched with the iCloud. If none is provided, will default to false + /// - returns: True if the save was successful, false otherwise. + @discardableResult open func set(_ value : T, forKey key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil, isSynchronizable: Bool = false) -> Bool { + guard let data = try? JSONEncoder().encode(value) else { return false } + + return set(data, forKey: key, withAccessibility: accessibility, isSynchronizable: isSynchronizable) + } /// Save a Data object to the keychain associated with a specified key. If data already exists for the given key, the data will be overwritten with the new value. /// diff --git a/SwiftKeychainWrapper/KeychainWrapperSubscript.swift b/SwiftKeychainWrapper/KeychainWrapperSubscript.swift index 4465718..84c8569 100644 --- a/SwiftKeychainWrapper/KeychainWrapperSubscript.swift +++ b/SwiftKeychainWrapper/KeychainWrapperSubscript.swift @@ -73,6 +73,14 @@ public extension KeychainWrapper { } #endif + subscript(key: Key) -> T? { + get { return codable(forKey: key) } + set { + guard let value = newValue else { return } + set(value, forKey: key.rawValue) + } + } + subscript(key: Key) -> Data? { get { return data(forKey: key) } set { @@ -86,6 +94,13 @@ public extension KeychainWrapper { public extension KeychainWrapper { + public func codable(forKey key: Key) -> T? { + if let value : T = codable(forKey: key.rawValue) { + return value + } + return nil + } + func data(forKey key: Key) -> Data? { if let value = data(forKey: key.rawValue) { return value