Evgenii Neumerzhitckii 9 anni fa
parent
commit
6cb11e2a79

+ 2 - 2
KeychainSwift/KeychainSwift.swift

@@ -25,8 +25,8 @@ public class KeychainSwift {
   
   /**
    
-  Specifies whether the items can be synchronized with other devices. Setting this property to true will
-   add the item to other devices with the `set` method, obtain synchronizable items with `get` command. Deleting synchronizable items will remove them from all devices.
+  Specifies whether the items can be synchronized with other devices through iCloud. Setting this property to true will
+   add the item to other devices with the `set` method and obtain synchronizable items with the `get` command. Deleting synchronizable items will remove them from all devices. In order for keychain synchronization to work the user must enable "Keychain" in iCloud settings.
    
   */
   public var synchronizable: Bool = false

+ 17 - 4
KeychainSwiftTests/SynchronizableTests.swift

@@ -16,25 +16,38 @@ class SynchronizableTests: XCTestCase {
   
   // MARK: - addSynchronizableIfRequired
   
-  func testAddSynchronizableGroup() {
+  func testAddSynchronizableGroup_addItemsFalse() {
     let items: [String: NSObject] = [
       "one": "two"
     ]
     
     obj.synchronizable = true
-    let result = obj.addSynchronizableIfRequired(items)
+    let result = obj.addSynchronizableIfRequired(items, addingItems: false)
     
     XCTAssertEqual(2, result.count)
     XCTAssertEqual("two", result["one"])
     XCTAssertEqual(kSecAttrSynchronizableAny, result["sync"])
   }
   
+  func testAddSynchronizableGroup_addItemsTrue() {
+    let items: [String: NSObject] = [
+      "one": "two"
+    ]
+    
+    obj.synchronizable = true
+    let result = obj.addSynchronizableIfRequired(items, addingItems: true)
+    
+    XCTAssertEqual(2, result.count)
+    XCTAssertEqual("two", result["one"])
+    XCTAssertEqual(true, result["sync"])
+  }
+  
   func testAddSynchronizableGroup_nil() {
     let items: [String: NSObject] = [
       "one": "two"
     ]
     
-    let result = obj.addSynchronizableIfRequired(items)
+    let result = obj.addSynchronizableIfRequired(items, addingItems: false)
     
     XCTAssertEqual(1, result.count)
     XCTAssertEqual("two", result["one"])
@@ -45,7 +58,7 @@ class SynchronizableTests: XCTestCase {
   func testSet() {
     obj.synchronizable = true
     obj.set("hello :)", forKey: "key 1")
-    XCTAssertEqual(kSecAttrSynchronizableAny, obj.lastQueryParameters?["sync"])
+    XCTAssertEqual(true, obj.lastQueryParameters?["sync"])
   }
   
   func testSet_doNotSetSynchronizable() {

+ 19 - 2
README.md

@@ -9,7 +9,7 @@
 
 This is a collection of helper functions for saving text and data in the Keychain.
  As you probably noticed Apple's keychain API is a bit verbose. This library was designed to provide shorter syntax for accomplishing a simple task: reading/writing text values for specified keys.
- 
+
 ## What's Keychain?
 
 Keychain is a secure storage. You can store all kind of sensitive data in it: user passwords, credit card numbers, secret tokens etc. Once stored in Keychain this information is only available to your app, other apps can't see it. Besides that, operating system makes sure this information is kept and processed securely. For example, text stored in Keychain can not be extracted from iPhone backup or from its file system. Apple recommends storing only small amount of data in the Keychain. If you need to secure something big you can encrypt it manually, save to a file and store the key in the Keychain.
@@ -110,7 +110,24 @@ You can use `.AccessibleAfterFirstUnlock` if you need your app to access the key
 
 See the list of all available [access options](https://github.com/marketplacer/keychain-swift/blob/master/KeychainSwift/KeychainSwiftAccessOptions.swift).
 
-### Sharing keychain items
+
+### Synchronizing keychain items with other devices
+
+Set `synchronizable` property to `true` to enable keychain items synchronization across user's multiple devices. In order for keychain synchronization to work the user must enable "Keychain" in iCloud settings on the device. Setting `synchronizable` to `true` will add the item to other devices with the `set` method and obtain synchronizable items with the `get` command. Deleting a synchronizable item will remove it from all devices.
+
+```Swift
+// First device
+let keychain = KeychainSwift()
+keychain.synchronizable = true
+keychain.set("hello world", forKey: "my key")
+
+// Second device
+let keychain = KeychainSwift()
+keychain.synchronizable = true
+keychain.get("my key") // Returns "hello world"
+```
+
+### Sharing keychain items with other apps
 
 In order to share keychain items between apps they need to have common *Keychain Groups* registered in *Capabilities > Keychain Sharing* settings. [This tutorial](http://evgenii.com/blog/sharing-keychain-in-ios/) shows how to set it up.