Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b9c0749257 | ||
|
|
d9814c2cc8 | ||
|
|
6d0ba0d767 | ||
|
|
0604692010 | ||
|
|
d2bc31815a | ||
|
|
15284371dc | ||
|
|
32ba85936c | ||
|
|
0c902f0842 | ||
|
|
7c4e5c205d | ||
|
|
40c4396b7e | ||
|
|
a85afe2413 | ||
|
|
b294163866 | ||
|
|
2f4fa2e61a | ||
|
|
51ee0ec8d7 | ||
|
|
ebd2234deb |
@@ -302,7 +302,7 @@
|
||||
"@executable_path/Frameworks",
|
||||
"@loader_path/Frameworks",
|
||||
);
|
||||
MARKETING_VERSION = 1.0.0;
|
||||
MARKETING_VERSION = 1.1.2;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = "com.mr-noone.keychain-kit";
|
||||
SKIP_INSTALL = YES;
|
||||
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
|
||||
@@ -327,7 +327,7 @@
|
||||
"@executable_path/Frameworks",
|
||||
"@loader_path/Frameworks",
|
||||
);
|
||||
MARKETING_VERSION = 1.0.0;
|
||||
MARKETING_VERSION = 1.1.2;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = "com.mr-noone.keychain-kit";
|
||||
SKIP_INSTALL = YES;
|
||||
SWIFT_VERSION = 5.0;
|
||||
|
||||
@@ -8,7 +8,21 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
public struct Keychain {
|
||||
public protocol KeychainProtocol {
|
||||
func get(_ key: String) throws -> Data
|
||||
func get(_ key: String) throws -> String
|
||||
func get(_ key: String) throws -> UUID
|
||||
func get<T>(_ key: String, decoder: JSONDecoder) throws -> T where T: Decodable
|
||||
|
||||
func set(_ data: Data, for key: String) throws
|
||||
func set(_ value: String, for key: String) throws
|
||||
func set(_ uuid: UUID, for key: String) throws
|
||||
func set<T>(_ value: T, for key: String, encoder: JSONEncoder) throws where T: Encodable
|
||||
|
||||
func delete(_ key: String) throws
|
||||
}
|
||||
|
||||
public struct Keychain: KeychainProtocol {
|
||||
public enum Error: Swift.Error {
|
||||
case noData
|
||||
case unexpectedData
|
||||
@@ -26,6 +40,7 @@ public struct Keychain {
|
||||
kSecClass : kSecClassGenericPassword,
|
||||
kSecAttrAccount : key as AnyObject,
|
||||
kSecMatchLimit : kSecMatchLimitOne,
|
||||
kSecReturnAttributes : kCFBooleanTrue,
|
||||
kSecReturnData : kCFBooleanTrue
|
||||
]
|
||||
|
||||
@@ -46,7 +61,17 @@ public struct Keychain {
|
||||
}
|
||||
|
||||
public func get(_ key: String) throws -> String {
|
||||
return String(data: try get(key), encoding: .utf8)!
|
||||
guard let value = String(data: try get(key), encoding: .utf8) else {
|
||||
throw Error.unexpectedData
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
public func get(_ key: String) throws -> UUID {
|
||||
guard let value = UUID(uuidString: try get(key)) else {
|
||||
throw Error.unexpectedData
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
public func get<T>(_ key: String, decoder: JSONDecoder = JSONDecoder()) throws -> T where T: Decodable {
|
||||
@@ -70,6 +95,10 @@ public struct Keychain {
|
||||
try set(value.data(using: .utf8)!, for: key)
|
||||
}
|
||||
|
||||
public func set(_ uuid: UUID, for key: String) throws {
|
||||
try set(uuid.uuidString, for: key)
|
||||
}
|
||||
|
||||
public func set<T>(_ value: T, for key: String, encoder: JSONEncoder = JSONEncoder()) throws where T: Encodable {
|
||||
try set(encoder.encode(value), for: key)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user