Browse Source

Change API

Evgenii Neumerzhitckii 10 years ago
parent
commit
fe2e8a1313

+ 2 - 2
README.md

@@ -14,7 +14,7 @@ Copy [TegKeychain.swift](https://raw.githubusercontent.com/exchangegroup/keychai
 ## Usage
 
 ```Swift
-TegKeychain.set("my key", value: "hello world")
+TegKeychain.set("hello world", forKey: "my key")
 
 TegKeychain.get("my key")
 
@@ -26,7 +26,7 @@ TegKeychain.clear() // delete everything from app's Keychain
 In addition to strings one can set/get `NSData` objects.
 
 ```Swift
-TegKeychain.set("my key", value: nsDataObject)
+TegKeychain.set(nsDataObject, forKey: "my key")
 
 TegKeychain.getData("my key")
 ```

+ 9 - 9
keychain/TegKeychain.swift

@@ -11,18 +11,18 @@ public class TegKeychain {
   
   /**
   
-  Store text value in the keychain item.
+  Stores the text value in the keychain item under the given key.
   
   :param: key Key under which the text value is stored in the keychain.
   :param: value Text string to be written to the keychain.
   :param: 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.
 
   */
-  public class func set(key: String, value: String,
+  public class func set(value: String, forKey key: String,
     withAccess access: TegKeychainAccessOptions? = nil) -> Bool {
     
-    if let data = value.dataUsingEncoding(NSUTF8StringEncoding) {
-      return set(key, value: data)
+    if let value = value.dataUsingEncoding(NSUTF8StringEncoding) {
+      return set(value, forKey: key)
     }
     
     return false
@@ -30,7 +30,7 @@ public class TegKeychain {
 
   /**
   
-  Store data in the keychain item.
+  Stores the data in the keychain item under the given key.
   
   :param: key Key under which the data is stored in the keychain.
   :param: value Data to be written to the keychain.
@@ -39,7 +39,7 @@ public class TegKeychain {
   :returns: True if the text was successfully written to the keychain.
   
   */
-  public class func set(key: String, value: NSData,
+  public class func set(value: NSData, forKey key: String,
     withAccess access: TegKeychainAccessOptions? = nil) -> Bool {
 
     let accessible = access?.value ?? TegKeychainAccessOptions.defaultOption.value
@@ -60,7 +60,7 @@ public class TegKeychain {
 
   /**
   
-  Retrieves the text value from the keychain.
+  Retrieves the text value from the keychain that corresponds to the given key.
   
   :param: key The key that is used to read the keychain item.
   :returns: The text value from the keychain. Returns nil if unable to read the item.
@@ -78,7 +78,7 @@ public class TegKeychain {
 
   /**
   
-  Retrieves the data from the keychain.
+  Retrieves the data from the keychain that corresponds to the given key.
   
   :param: key The key that is used to read the keychain item.
   :returns: The text value from the keychain. Returns nil if unable to read the item.
@@ -104,7 +104,7 @@ public class TegKeychain {
 
   /**
   
-  Deletes the keychain item.
+  Deletes the single keychain item specified by the key.
   
   :param: key The key that is used to delete the keychain item.
   :returns: True if the item was successfully deleted.

+ 1 - 1
keychain/TegKeychainAccessOptions.swift

@@ -2,7 +2,7 @@ import Security
 
 /**
 
-These constants are use to determining when a keychain item should be readable. The default value is AccessibleWhenUnlocked.
+These options are used to determine when a keychain item should be readable. The default value is AccessibleWhenUnlocked.
 
 */
 public enum TegKeychainAccessOptions {

+ 1 - 1
keychain/ViewController.swift

@@ -20,7 +20,7 @@ class ViewController: UIViewController {
   }
 
   @IBAction func onSaveTapped(sender: AnyObject) {
-    TegKeychain.set(TegKeychainDemo_keyName, value: textField.text)
+    TegKeychain.set(textField.text, forKey: TegKeychainDemo_keyName)
     updateValueLabel()
   }
 

+ 7 - 7
keychainTests/keychainTests.swift

@@ -13,12 +13,12 @@ class keychainTests: XCTestCase {
   // -----------------------
 
   func testSet() {
-    XCTAssertTrue(TegKeychain.set("key 1", value: "hello :)"))
+    XCTAssertTrue(TegKeychain.set("hello :)", forKey: "key 1"))
     XCTAssertEqual("hello :)", TegKeychain.get("key 1")!)
   }
   
   func testSetWithOptions() {
-    TegKeychain.set("key 1", value: "hello :)", withAccess: .AccessibleAfterFirstUnlock)
+    TegKeychain.set("hello :)", forKey: "key 1", withAccess: .AccessibleAfterFirstUnlock)
   }
 
   // Get
@@ -32,15 +32,15 @@ class keychainTests: XCTestCase {
   // -----------------------
 
   func testDelete() {
-    TegKeychain.set("key 1", value: "hello :)")
+    TegKeychain.set("hello :)", forKey: "key 1")
     TegKeychain.delete("key 1")
     
     XCTAssert(TegKeychain.get("key 1") == nil)
   }
 
   func testDelete_deleteOnSingleKey() {
-    TegKeychain.set("key 1", value: "hello :)")
-    TegKeychain.set("key 2", value: "hello two")
+    TegKeychain.set("hello :)", forKey: "key 1")
+    TegKeychain.set("hello two", forKey: "key 2")
 
     TegKeychain.delete("key 1")
     
@@ -51,8 +51,8 @@ class keychainTests: XCTestCase {
   // -----------------------
 
   func testClear() {
-    TegKeychain.set("key 1", value: "hello :)")
-    TegKeychain.set("key 2", value: "hello two")
+    TegKeychain.set("hello :)", forKey: "key 1")
+    TegKeychain.set("hello two", forKey: "key 2")
     
     TegKeychain.clear()