Forráskód Böngészése

[Feature] Added able to get data reference

Medium 6 éve
szülő
commit
bdc8ab51c6
3 módosított fájl, 21 hozzáadás és 3 törlés
  1. 7 0
      README.md
  2. 11 3
      Sources/KeychainSwift.swift
  3. 3 0
      Sources/TegKeychainConstants.swift

+ 7 - 0
README.md

@@ -103,6 +103,13 @@ keychain.set(dataObject, forKey: "my key")
 keychain.getData("my key")
 ```
 
+#### Reference to value
+
+```Swift
+let keychain = KeychainSwift()
+keychain.set(dataObject, forKey: "my key")
+keychain.getData("my key", isReference: true)
+
 #### Removing keys from Keychain
 
 ```Swift

+ 11 - 3
Sources/KeychainSwift.swift

@@ -153,10 +153,11 @@ open class KeychainSwift {
   Retrieves the data from the keychain that corresponds to the given key.
   
   - parameter key: The key that is used to read the keychain item.
+  - parameter isReference: Flag is used to determine the returning value (reference to the value or value)
   - returns: The text value from the keychain. Returns nil if unable to read the item.
   
   */
-  open func getData(_ key: String) -> Data? {
+  open func getData(_ key: String, isReference: Bool = false) -> Data? {
     // The lock prevents the code to be run simlultaneously
     // from multiple threads which may result in crashing
     readLock.lock()
@@ -167,10 +168,15 @@ open class KeychainSwift {
     var query: [String: Any] = [
       KeychainSwiftConstants.klass       : kSecClassGenericPassword,
       KeychainSwiftConstants.attrAccount : prefixedKey,
-      KeychainSwiftConstants.returnData  : kCFBooleanTrue,
       KeychainSwiftConstants.matchLimit  : kSecMatchLimitOne
     ]
     
+    if isReference {
+      query[KeychainSwiftConstants.returnRefernce] = kCFBooleanTrue
+    } else {
+      query[KeychainSwiftConstants.returnData] =  kCFBooleanTrue
+    }
+    
     query = addAccessGroupWhenPresent(query)
     query = addSynchronizableIfRequired(query, addingItems: false)
     lastQueryParameters = query
@@ -181,7 +187,9 @@ open class KeychainSwift {
       SecItemCopyMatching(query as CFDictionary, UnsafeMutablePointer($0))
     }
     
-    if lastResultCode == noErr { return result as? Data }
+    if lastResultCode == noErr {
+      return result as? Data
+    }
     
     return nil
   }

+ 3 - 0
Sources/TegKeychainConstants.swift

@@ -30,6 +30,9 @@ public struct KeychainSwiftConstants {
   
   /// Used for specifying a value when setting a Keychain value.
   public static var valueData: String { return toString(kSecValueData) }
+    
+  /// Used for specifying a reference when setting a Keychain value.
+  public static var returnRefernce: String { return toString(kSecReturnPersistentRef) }
   
   static func toString(_ value: CFString) -> String {
     return value as String