|
|
@@ -24,6 +24,9 @@ A collection of helper functions for saving text and data in the keychain.
|
|
|
public class KeychainSwift {
|
|
|
|
|
|
var lastQueryParameters: [String: NSObject]? // Used by the unit tests
|
|
|
+
|
|
|
+ /// Contains result code from the last operation. Value is noErr (0) for a successful result.
|
|
|
+ public var lastResultCode: OSStatus = noErr
|
|
|
|
|
|
var keyPrefix = "" // Can be useful in test.
|
|
|
|
|
|
@@ -52,6 +55,8 @@ public class KeychainSwift {
|
|
|
- parameter key: Key under which the text value is stored in the keychain.
|
|
|
- parameter value: Text string to be written to the keychain.
|
|
|
- parameter withAccess: Value that indicates when your app needs access to the text in the keychain item. By default the .AccessibleWhenUnlocked option is used that permits the data to be accessed only while the device is unlocked by the user.
|
|
|
+
|
|
|
+ - returns: True if the text was successfully written to the keychain.
|
|
|
|
|
|
*/
|
|
|
public func set(value: String, forKey key: String,
|
|
|
@@ -94,9 +99,9 @@ public class KeychainSwift {
|
|
|
query = addAccessGroupWhenPresent(query)
|
|
|
lastQueryParameters = query
|
|
|
|
|
|
- let status: OSStatus = SecItemAdd(query as CFDictionaryRef, nil)
|
|
|
+ lastResultCode = SecItemAdd(query as CFDictionaryRef, nil)
|
|
|
|
|
|
- return status == noErr
|
|
|
+ return lastResultCode == noErr
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -108,10 +113,12 @@ public class KeychainSwift {
|
|
|
|
|
|
*/
|
|
|
public func get(key: String) -> String? {
|
|
|
- if let data = getData(key),
|
|
|
- let currentString = NSString(data: data, encoding: NSUTF8StringEncoding) as? String {
|
|
|
-
|
|
|
- return currentString
|
|
|
+ if let data = getData(key) {
|
|
|
+ if let currentString = NSString(data: data, encoding: NSUTF8StringEncoding) as? String {
|
|
|
+ return currentString
|
|
|
+ }
|
|
|
+
|
|
|
+ lastResultCode = -67853 // errSecInvalidEncoding
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
@@ -139,11 +146,11 @@ public class KeychainSwift {
|
|
|
|
|
|
var result: AnyObject?
|
|
|
|
|
|
- let status = withUnsafeMutablePointer(&result) {
|
|
|
+ lastResultCode = withUnsafeMutablePointer(&result) {
|
|
|
SecItemCopyMatching(query, UnsafeMutablePointer($0))
|
|
|
}
|
|
|
|
|
|
- if status == noErr { return result as? NSData }
|
|
|
+ if lastResultCode == noErr { return result as? NSData }
|
|
|
|
|
|
return nil
|
|
|
}
|
|
|
@@ -166,9 +173,9 @@ public class KeychainSwift {
|
|
|
query = addAccessGroupWhenPresent(query)
|
|
|
lastQueryParameters = query
|
|
|
|
|
|
- let status: OSStatus = SecItemDelete(query as CFDictionaryRef)
|
|
|
+ lastResultCode = SecItemDelete(query as CFDictionaryRef)
|
|
|
|
|
|
- return status == noErr
|
|
|
+ return lastResultCode == noErr
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -183,9 +190,9 @@ public class KeychainSwift {
|
|
|
query = addAccessGroupWhenPresent(query)
|
|
|
lastQueryParameters = query
|
|
|
|
|
|
- let status: OSStatus = SecItemDelete(query as CFDictionaryRef)
|
|
|
+ lastResultCode = SecItemDelete(query as CFDictionaryRef)
|
|
|
|
|
|
- return status == noErr
|
|
|
+ return lastResultCode == noErr
|
|
|
}
|
|
|
|
|
|
/// Returns the key with currently set prefix.
|