|
|
@@ -54,10 +54,11 @@ public class KeychainSwift {
|
|
|
- returns: True if the text was successfully written to the keychain.
|
|
|
|
|
|
*/
|
|
|
- public func set(value: String, forKey key: String,
|
|
|
+ @discardableResult
|
|
|
+ public func set(_ value: String, forKey key: String,
|
|
|
withAccess access: KeychainSwiftAccessOptions? = nil) -> Bool {
|
|
|
|
|
|
- if let value = value.dataUsingEncoding(NSUTF8StringEncoding) {
|
|
|
+ if let value = value.data(using: String.Encoding.utf8) {
|
|
|
return set(value, forKey: key, withAccess: access)
|
|
|
}
|
|
|
|
|
|
@@ -75,10 +76,11 @@ public class KeychainSwift {
|
|
|
- returns: True if the text was successfully written to the keychain.
|
|
|
|
|
|
*/
|
|
|
- public func set(value: NSData, forKey key: String,
|
|
|
+ @discardableResult
|
|
|
+ public func set(_ value: Data, forKey key: String,
|
|
|
withAccess access: KeychainSwiftAccessOptions? = nil) -> Bool {
|
|
|
|
|
|
- delete(key) // Delete any existing key before saving it
|
|
|
+ _ = delete(key) // Delete any existing key before saving it
|
|
|
|
|
|
let accessible = access?.value ?? KeychainSwiftAccessOptions.defaultOption.value
|
|
|
|
|
|
@@ -91,11 +93,11 @@ public class KeychainSwift {
|
|
|
KeychainSwiftConstants.accessible : accessible
|
|
|
]
|
|
|
|
|
|
- query = addAccessGroupWhenPresent(query)
|
|
|
- query = addSynchronizableIfRequired(query, addingItems: true)
|
|
|
- lastQueryParameters = query
|
|
|
+ query = addAccessGroupWhenPresent(query as! [String : NSObject])
|
|
|
+ query = addSynchronizableIfRequired(query as! [String : NSObject], addingItems: true)
|
|
|
+ lastQueryParameters = query as? [String: NSObject]
|
|
|
|
|
|
- lastResultCode = SecItemAdd(query as CFDictionaryRef, nil)
|
|
|
+ lastResultCode = SecItemAdd(query as CFDictionary, nil)
|
|
|
|
|
|
return lastResultCode == noErr
|
|
|
}
|
|
|
@@ -111,11 +113,13 @@ public class KeychainSwift {
|
|
|
- returns: True if the value was successfully written to the keychain.
|
|
|
|
|
|
*/
|
|
|
- public func set(value: Bool, forKey key: String,
|
|
|
+ @discardableResult
|
|
|
+ public func set(_ value: Bool, forKey key: String,
|
|
|
withAccess access: KeychainSwiftAccessOptions? = nil) -> Bool {
|
|
|
|
|
|
- var localValue = value
|
|
|
- let data = NSData(bytes: &localValue, length: sizeof(Bool))
|
|
|
+ var bits = unsafeBitCast(value, to: UInt8.self)
|
|
|
+ let data = Data(bytes: &bits, count: sizeof(Bool))
|
|
|
+
|
|
|
return set(data, forKey: key, withAccess: access)
|
|
|
}
|
|
|
|
|
|
@@ -127,9 +131,9 @@ public class KeychainSwift {
|
|
|
- returns: The text value from the keychain. Returns nil if unable to read the item.
|
|
|
|
|
|
*/
|
|
|
- public func get(key: String) -> String? {
|
|
|
+ public func get(_ key: String) -> String? {
|
|
|
if let data = getData(key) {
|
|
|
- if let currentString = NSString(data: data, encoding: NSUTF8StringEncoding) as? String {
|
|
|
+ if let currentString = NSString(data: data, encoding: String.Encoding.utf8.rawValue) as? String {
|
|
|
return currentString
|
|
|
}
|
|
|
|
|
|
@@ -147,7 +151,7 @@ public class KeychainSwift {
|
|
|
- returns: The text value from the keychain. Returns nil if unable to read the item.
|
|
|
|
|
|
*/
|
|
|
- public func getData(key: String) -> NSData? {
|
|
|
+ public func getData(_ key: String) -> Data? {
|
|
|
let prefixedKey = keyWithPrefix(key)
|
|
|
|
|
|
var query: [String: NSObject] = [
|
|
|
@@ -167,7 +171,7 @@ public class KeychainSwift {
|
|
|
SecItemCopyMatching(query, UnsafeMutablePointer($0))
|
|
|
}
|
|
|
|
|
|
- if lastResultCode == noErr { return result as? NSData }
|
|
|
+ if lastResultCode == noErr { return result as? Data }
|
|
|
|
|
|
return nil
|
|
|
}
|
|
|
@@ -180,10 +184,10 @@ public class KeychainSwift {
|
|
|
- returns: The boolean value from the keychain. Returns nil if unable to read the item.
|
|
|
|
|
|
*/
|
|
|
- public func getBool(key: String) -> Bool? {
|
|
|
+ public func getBool(_ key: String) -> Bool? {
|
|
|
guard let data = getData(key) else { return nil }
|
|
|
var boolValue = false
|
|
|
- data.getBytes(&boolValue, length: sizeof(Bool))
|
|
|
+ (data as NSData).getBytes(&boolValue, length: sizeof(Bool))
|
|
|
return boolValue
|
|
|
}
|
|
|
|
|
|
@@ -195,7 +199,8 @@ public class KeychainSwift {
|
|
|
- returns: True if the item was successfully deleted.
|
|
|
|
|
|
*/
|
|
|
- public func delete(key: String) -> Bool {
|
|
|
+ @discardableResult
|
|
|
+ public func delete(_ key: String) -> Bool {
|
|
|
let prefixedKey = keyWithPrefix(key)
|
|
|
|
|
|
var query: [String: NSObject] = [
|
|
|
@@ -207,7 +212,7 @@ public class KeychainSwift {
|
|
|
query = addSynchronizableIfRequired(query, addingItems: false)
|
|
|
lastQueryParameters = query
|
|
|
|
|
|
- lastResultCode = SecItemDelete(query as CFDictionaryRef)
|
|
|
+ lastResultCode = SecItemDelete(query as CFDictionary)
|
|
|
|
|
|
return lastResultCode == noErr
|
|
|
}
|
|
|
@@ -219,23 +224,24 @@ public class KeychainSwift {
|
|
|
- returns: True if the keychain items were successfully deleted.
|
|
|
|
|
|
*/
|
|
|
+ @discardableResult
|
|
|
public func clear() -> Bool {
|
|
|
var query: [String: NSObject] = [ kSecClass as String : kSecClassGenericPassword ]
|
|
|
query = addAccessGroupWhenPresent(query)
|
|
|
query = addSynchronizableIfRequired(query, addingItems: false)
|
|
|
lastQueryParameters = query
|
|
|
|
|
|
- lastResultCode = SecItemDelete(query as CFDictionaryRef)
|
|
|
+ lastResultCode = SecItemDelete(query as CFDictionary)
|
|
|
|
|
|
return lastResultCode == noErr
|
|
|
}
|
|
|
|
|
|
/// Returns the key with currently set prefix.
|
|
|
- func keyWithPrefix(key: String) -> String {
|
|
|
+ func keyWithPrefix(_ key: String) -> String {
|
|
|
return "\(keyPrefix)\(key)"
|
|
|
}
|
|
|
|
|
|
- func addAccessGroupWhenPresent(items: [String: NSObject]) -> [String: NSObject] {
|
|
|
+ func addAccessGroupWhenPresent(_ items: [String: NSObject]) -> [String: NSObject] {
|
|
|
guard let accessGroup = accessGroup else { return items }
|
|
|
|
|
|
var result: [String: NSObject] = items
|
|
|
@@ -253,10 +259,10 @@ public class KeychainSwift {
|
|
|
- returns: the dictionary with kSecAttrSynchronizable item added if it was requested. Otherwise, it returns the original dictionary.
|
|
|
|
|
|
*/
|
|
|
- func addSynchronizableIfRequired(items: [String: NSObject], addingItems: Bool) -> [String: NSObject] {
|
|
|
+ func addSynchronizableIfRequired(_ items: [String: NSObject], addingItems: Bool) -> [String: NSObject] {
|
|
|
if !synchronizable { return items }
|
|
|
var result: [String: NSObject] = items
|
|
|
result[KeychainSwiftConstants.attrSynchronizable] = addingItems == true ? true : kSecAttrSynchronizableAny
|
|
|
return result
|
|
|
}
|
|
|
-}
|
|
|
+}
|