Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Codable saving functionality #167

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions SwiftKeychainWrapper/KeychainWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<T : 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.
///
Expand Down Expand Up @@ -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<T : Codable>(_ 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.
///
Expand Down
15 changes: 15 additions & 0 deletions SwiftKeychainWrapper/KeychainWrapperSubscript.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ public extension KeychainWrapper {
}
#endif

subscript<T : Codable>(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 {
Expand All @@ -86,6 +94,13 @@ public extension KeychainWrapper {

public extension KeychainWrapper {

public func codable<T : 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
Expand Down