Procházet zdrojové kódy

Add missing documentation

Evgenii Neumerzhitckii před 10 roky
rodič
revize
29f0709f54

+ 7 - 3
CHANGELOG.md

@@ -1,9 +1,13 @@
 # KeychainSwift version history
 
-## 3.0.8 (2015-11-02)
+## 3.0.11 (2016-01-24)
 
-Added `lastResultCode` property.
+Added methods for setting/getting booleans.
 
 ## 3.0.9 (2015-11-09)
 
-Moved repository to https://github.com/marketplacer/keychain-swift
+Moved repository to https://github.com/marketplacer/keychain-swift
+
+## 3.0.8 (2015-11-02)
+
+Added `lastResultCode` property.

+ 59 - 13
Distrib/KeychainSwiftDistrib.swift

@@ -37,6 +37,7 @@ public class KeychainSwift {
   */
   public var accessGroup: String?
   
+  /// Instantiate a KeychainSwift object
   public init() { }
   
   /**
@@ -90,7 +91,7 @@ public class KeychainSwift {
     let prefixedKey = keyWithPrefix(key)
       
     var query = [
-      KeychainSwiftConstants.klass       : KeychainSwiftConstants.classGenericPassword,
+      KeychainSwiftConstants.klass       : kSecClassGenericPassword,
       KeychainSwiftConstants.attrAccount : prefixedKey,
       KeychainSwiftConstants.valueData   : value,
       KeychainSwiftConstants.accessible  : accessible
@@ -104,6 +105,25 @@ public class KeychainSwift {
     return lastResultCode == noErr
   }
 
+  /**
+
+  Stores the boolean value in the keychain item under the given key.
+
+  - parameter key: Key under which the value is stored in the keychain.
+  - parameter value: Boolean to be written to the keychain.
+  - parameter withAccess: Value that indicates when your app needs access to the value 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 value was successfully written to the keychain.
+
+  */
+  public func set(value: Bool, forKey key: String,
+    withAccess access: KeychainSwiftAccessOptions? = nil) -> Bool {
+
+    var localValue = value
+    let data = NSData(bytes: &localValue, length: sizeof(Bool))
+    return set(data, forKey: key, withAccess: access)
+  }
+
   /**
   
   Retrieves the text value from the keychain that corresponds to the given key.
@@ -156,7 +176,22 @@ public class KeychainSwift {
   }
 
   /**
-  
+
+  Retrieves the boolean value from the keychain that corresponds to the given key.
+
+  - parameter key: The key that is used to read the keychain item.
+  - returns: The boolean value from the keychain. Returns nil if unable to read the item.
+
+  */
+  public func getBool(key: String) -> Bool? {
+    guard let data = getData(key) else { return nil }
+    var boolValue = false
+    data.getBytes(&boolValue, length: sizeof(Bool))
+    return boolValue
+  }
+
+  /**
+
   Deletes the single keychain item specified by the key.
   
   - parameter key: The key that is used to delete the keychain item.
@@ -334,22 +369,33 @@ public enum KeychainSwiftAccessOptions {
 import Foundation
 import Security
 
+/// Constants used by the library
 public struct KeychainSwiftConstants {
-  public static var klass: String { return toString(kSecClass) }
-  public static var classGenericPassword: String { return toString(kSecClassGenericPassword) }
-  public static var attrAccount: String { return toString(kSecAttrAccount) }
-  public static var valueData: String { return toString(kSecValueData) }
-  public static var returnData: String { return toString(kSecReturnData) }
-  public static var matchLimit: String { return toString(kSecMatchLimit) }
+  /// Specifies a Keychain access group. Used for sharing Keychain items between apps.
   public static var accessGroup: String { return toString(kSecAttrAccessGroup) }
-
+  
   /**
+   
+   A value that indicates when your app needs access to the data in a keychain item. The default value is AccessibleWhenUnlocked. For a list of possible values, see KeychainSwiftAccessOptions.
+   
+   */
+  public static var accessible: String { return toString(kSecAttrAccessible) }
   
-  A value that indicates when your app needs access to the data in a keychain item. The default value is AccessibleWhenUnlocked. For a list of possible values, see KeychainSwiftAccessOptions.
+  /// Used for specifying a String key when setting/getting a Keychain value.
+  public static var attrAccount: String { return toString(kSecAttrAccount) }
+  
+  /// An item class key used to construct a Keychain search dictionary.
+  public static var klass: String { return toString(kSecClass) }
+  
+  /// Specifies the number of values returned from the keychain. The library only supports single values.
+  public static var matchLimit: String { return toString(kSecMatchLimit) }
+  
+  /// A return data type used to get the data from the Keychain.
+  public static var returnData: String { return toString(kSecReturnData) }
+  
+  /// Used for specifying a value when setting a Keychain value.
+  public static var valueData: String { return toString(kSecValueData) }
   
-  */
-  public static var accessible: String { return toString(kSecAttrAccessible) }
-
   static func toString(value: CFStringRef) -> String {
     return value as String
   }

+ 3 - 4
KeychainSwift/KeychainSwift.swift

@@ -22,6 +22,7 @@ public class KeychainSwift {
   */
   public var accessGroup: String?
   
+  /// Instantiate a KeychainSwift object
   public init() { }
   
   /**
@@ -75,7 +76,7 @@ public class KeychainSwift {
     let prefixedKey = keyWithPrefix(key)
       
     var query = [
-      KeychainSwiftConstants.klass       : KeychainSwiftConstants.classGenericPassword,
+      KeychainSwiftConstants.klass       : kSecClassGenericPassword,
       KeychainSwiftConstants.attrAccount : prefixedKey,
       KeychainSwiftConstants.valueData   : value,
       KeychainSwiftConstants.accessible  : accessible
@@ -168,9 +169,7 @@ public class KeychainSwift {
 
   */
   public func getBool(key: String) -> Bool? {
-    guard let data = getData(key) else {
-      return nil
-    }
+    guard let data = getData(key) else { return nil }
     var boolValue = false
     data.getBytes(&boolValue, length: sizeof(Bool))
     return boolValue

+ 22 - 11
KeychainSwift/TegKeychainConstants.swift

@@ -1,22 +1,33 @@
 import Foundation
 import Security
 
+/// Constants used by the library
 public struct KeychainSwiftConstants {
-  public static var klass: String { return toString(kSecClass) }
-  public static var classGenericPassword: String { return toString(kSecClassGenericPassword) }
-  public static var attrAccount: String { return toString(kSecAttrAccount) }
-  public static var valueData: String { return toString(kSecValueData) }
-  public static var returnData: String { return toString(kSecReturnData) }
-  public static var matchLimit: String { return toString(kSecMatchLimit) }
+  /// Specifies a Keychain access group. Used for sharing Keychain items between apps.
   public static var accessGroup: String { return toString(kSecAttrAccessGroup) }
-
+  
   /**
+   
+   A value that indicates when your app needs access to the data in a keychain item. The default value is AccessibleWhenUnlocked. For a list of possible values, see KeychainSwiftAccessOptions.
+   
+   */
+  public static var accessible: String { return toString(kSecAttrAccessible) }
   
-  A value that indicates when your app needs access to the data in a keychain item. The default value is AccessibleWhenUnlocked. For a list of possible values, see KeychainSwiftAccessOptions.
+  /// Used for specifying a String key when setting/getting a Keychain value.
+  public static var attrAccount: String { return toString(kSecAttrAccount) }
+  
+  /// An item class key used to construct a Keychain search dictionary.
+  public static var klass: String { return toString(kSecClass) }
+  
+  /// Specifies the number of values returned from the keychain. The library only supports single values.
+  public static var matchLimit: String { return toString(kSecMatchLimit) }
+  
+  /// A return data type used to get the data from the Keychain.
+  public static var returnData: String { return toString(kSecReturnData) }
+  
+  /// Used for specifying a value when setting a Keychain value.
+  public static var valueData: String { return toString(kSecValueData) }
   
-  */
-  public static var accessible: String { return toString(kSecAttrAccessible) }
-
   static func toString(value: CFStringRef) -> String {
     return value as String
   }

+ 0 - 1
KeychainSwiftTests/KeychainSwiftTests.swift

@@ -68,7 +68,6 @@ class keychainTests: XCTestCase {
 
   func testSetBool_usesAccessibleWhenUnlockedByDefault() {
     XCTAssertTrue(obj.set(false, forKey: "key bool"))
-
     let accessValue = obj.lastQueryParameters?[KeychainSwiftConstants.accessible] as? String
     XCTAssertEqual(KeychainSwiftAccessOptions.AccessibleWhenUnlocked.value, accessValue!)
   }

+ 22 - 4
README.md

@@ -51,19 +51,28 @@ Use the [previous version of the library](https://github.com/marketplacer/keycha
 
 Add `import KeychainSwift` to your source code if you used Carthage or CocoaPods setup methods.
 
+#### String values
+
 ```Swift
 let keychain = KeychainSwift()
 
 keychain.set("hello world", forKey: "my key")
 
 keychain.get("my key")
+```
 
-keychain.delete("my key")
+#### Boolean values
 
-keychain.clear() // Delete everything from app's Keychain
+
+```Swift
+let keychain = KeychainSwift()
+
+keychain.set(true, forKey: "my key")
+
+keychain.getBool("my key")
 ```
 
-In addition to strings one can set/get `NSData` objects.
+#### NSData values
 
 ```Swift
 let keychain = KeychainSwift()
@@ -73,6 +82,14 @@ keychain.set(nsDataObject, forKey: "my key")
 keychain.getData("my key")
 ```
 
+#### Removing keys from Keychain
+
+```Swift
+keychain.delete("my key")
+
+keychain.clear() // Delete everything from app's Keychain
+```
+
 ## Advanced options
 
 ### Keychain item access
@@ -157,7 +174,8 @@ Here are some other Keychain libraries.
 ## Credits
 
 * The code is based on this example: [https://gist.github.com/s-aska/e7ad24175fb7b04f78e7](https://gist.github.com/s-aska/e7ad24175fb7b04f78e7)
-* Huge thanks to [pepibumur](https://github.com/pepibumur) for adding OS X, watchOS and tvOS support.
+* Thanks to [glyuck](https://github.com/glyuck) for taming booleans.
+* Thanks to [pepibumur](https://github.com/pepibumur) for adding OS X, watchOS and tvOS support.
 
 ## License