Przeglądaj źródła

Synchronizing keychain

Evgenii Neumerzhitckii 9 lat temu
rodzic
commit
39c1f88a07
2 zmienionych plików z 16 dodań i 19 usunięć
  1. 0 12
      Demo/ViewController.swift
  2. 16 7
      KeychainSwift/KeychainSwift.swift

+ 0 - 12
Demo/ViewController.swift

@@ -13,24 +13,15 @@ class ViewController: UIViewController {
   
   let keychain = KeychainSwift()
   
-  static let accessGroup = "synch keychain group"
-  
   override func viewDidLoad() {
     super.viewDidLoad()
     
     updateValueLabel()
   }
   
-  override func didReceiveMemoryWarning() {
-    super.didReceiveMemoryWarning()
-    // Dispose of any resources that can be recreated.
-  }
-  
   @IBAction func onSaveTapped(sender: AnyObject) {
     if let text = textField.text {
       keychain.synchronizable = synchronizableSwitch.on
-//      keychain.accessGroup = synchronizableSwitch.on ? ViewController.accessGroup : nil
-      
       keychain.set(text, forKey: TegKeychainDemo_keyName)
       updateValueLabel()
     }
@@ -38,8 +29,6 @@ class ViewController: UIViewController {
   
   @IBAction func onDeleteTapped(sender: AnyObject) {
     keychain.synchronizable = synchronizableSwitch.on
-//    keychain.accessGroup = synchronizableSwitch.on ? ViewController.accessGroup : nil
-
     keychain.delete(TegKeychainDemo_keyName)
     updateValueLabel()
   }
@@ -50,7 +39,6 @@ class ViewController: UIViewController {
   
   private func updateValueLabel() {
     keychain.synchronizable = synchronizableSwitch.on
-//    keychain.accessGroup = synchronizableSwitch.on ? ViewController.accessGroup : nil
     
     if let value = keychain.get(TegKeychainDemo_keyName) {
       valueLabel.text = "In Keychain: \(value)"

+ 16 - 7
KeychainSwift/KeychainSwift.swift

@@ -92,7 +92,7 @@ public class KeychainSwift {
     ]
       
     query = addAccessGroupWhenPresent(query)
-    query = addSynchronizableIfRequired(query)
+    query = addSynchronizableIfRequired(query, addingItems: true)
     lastQueryParameters = query
     
     lastResultCode = SecItemAdd(query as CFDictionaryRef, nil)
@@ -158,7 +158,7 @@ public class KeychainSwift {
     ]
     
     query = addAccessGroupWhenPresent(query)
-    query = addSynchronizableIfRequired(query)
+    query = addSynchronizableIfRequired(query, addingItems: false)
     lastQueryParameters = query
     
     var result: AnyObject?
@@ -204,7 +204,7 @@ public class KeychainSwift {
     ]
     
     query = addAccessGroupWhenPresent(query)
-    query = addSynchronizableIfRequired(query)
+    query = addSynchronizableIfRequired(query, addingItems: false)
     lastQueryParameters = query
     
     lastResultCode = SecItemDelete(query as CFDictionaryRef)
@@ -222,7 +222,7 @@ public class KeychainSwift {
   public func clear() -> Bool {
     var query: [String: NSObject] = [ kSecClass as String : kSecClassGenericPassword ]
     query = addAccessGroupWhenPresent(query)
-    query = addSynchronizableIfRequired(query)
+    query = addSynchronizableIfRequired(query, addingItems: false)
     lastQueryParameters = query
     
     lastResultCode = SecItemDelete(query as CFDictionaryRef)
@@ -243,11 +243,20 @@ public class KeychainSwift {
     return result
   }
   
-  /// Adds kSecAttrSynchronizable: kSecAttrSynchronizableAny` item to the dictionary when the `synchronizable` property is true.
-  func addSynchronizableIfRequired(items: [String: NSObject]) -> [String: NSObject] {
+  /**
+ 
+  Adds kSecAttrSynchronizable: kSecAttrSynchronizableAny` item to the dictionary when the `synchronizable` property is true.
+   
+   - parameter items: The dictionary where the kSecAttrSynchronizable items will be added when requested.
+   - parameter addingItems: Use `true` when the dictionary will be used with `SecItemAdd` method (adding a keychain item). For getting and deleting items, use `false`.
+   
+   - 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] {
     if !synchronizable { return items }
     var result: [String: NSObject] = items
-    result[KeychainSwiftConstants.attrSynchronizable] = kSecAttrSynchronizableAny
+    result[KeychainSwiftConstants.attrSynchronizable] = addingItems == true ? true : kSecAttrSynchronizableAny
     return result
   }
 }