KeychainSwift.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import UIKit
  2. import Security
  3. /**
  4. A collection of helper functions for saving text and data in the keychain.
  5. */
  6. public class KeychainSwift {
  7. static var lastQueryParameters: [String: NSObject]? // Used by unit tests
  8. /**
  9. Stores the text value in the keychain item under the given key.
  10. - parameter key: Key under which the text value is stored in the keychain.
  11. - parameter value: Text string to be written to the keychain.
  12. - parameter 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.
  13. */
  14. public class func set(value: String, forKey key: String,
  15. withAccess access: KeychainSwiftAccessOptions? = nil) -> Bool {
  16. if let value = value.dataUsingEncoding(NSUTF8StringEncoding) {
  17. return set(value, forKey: key, withAccess: access)
  18. }
  19. return false
  20. }
  21. /**
  22. Stores the data in the keychain item under the given key.
  23. - parameter key: Key under which the data is stored in the keychain.
  24. - parameter value: Data to be written to the keychain.
  25. - parameter 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.
  26. - returns: True if the text was successfully written to the keychain.
  27. */
  28. public class func set(value: NSData, forKey key: String,
  29. withAccess access: KeychainSwiftAccessOptions? = nil) -> Bool {
  30. let accessible = access?.value ?? KeychainSwiftAccessOptions.defaultOption.value
  31. let query = [
  32. KeychainSwiftConstants.klass : KeychainSwiftConstants.classGenericPassword,
  33. KeychainSwiftConstants.attrAccount : key,
  34. KeychainSwiftConstants.valueData : value,
  35. KeychainSwiftConstants.accessible : accessible
  36. ]
  37. lastQueryParameters = query
  38. SecItemDelete(query as CFDictionaryRef)
  39. let status: OSStatus = SecItemAdd(query as CFDictionaryRef, nil)
  40. return status == noErr
  41. }
  42. /**
  43. Retrieves the text value from the keychain that corresponds to the given key.
  44. - parameter key: The key that is used to read the keychain item.
  45. - returns: The text value from the keychain. Returns nil if unable to read the item.
  46. */
  47. public class func get(key: String) -> String? {
  48. if let data = getData(key),
  49. let currentString = NSString(data: data, encoding: NSUTF8StringEncoding) as? String {
  50. return currentString
  51. }
  52. return nil
  53. }
  54. /**
  55. Retrieves the data from the keychain that corresponds to the given key.
  56. - parameter key: The key that is used to read the keychain item.
  57. - returns: The text value from the keychain. Returns nil if unable to read the item.
  58. */
  59. public class func getData(key: String) -> NSData? {
  60. let query = [
  61. KeychainSwiftConstants.klass : kSecClassGenericPassword,
  62. KeychainSwiftConstants.attrAccount : key,
  63. KeychainSwiftConstants.returnData : kCFBooleanTrue,
  64. KeychainSwiftConstants.matchLimit : kSecMatchLimitOne ]
  65. var result: AnyObject?
  66. let status = withUnsafeMutablePointer(&result) {
  67. SecItemCopyMatching(query, UnsafeMutablePointer($0))
  68. }
  69. if status == noErr { return result as? NSData }
  70. return nil
  71. }
  72. /**
  73. Deletes the single keychain item specified by the key.
  74. - parameter key: The key that is used to delete the keychain item.
  75. - returns: True if the item was successfully deleted.
  76. */
  77. public class func delete(key: String) -> Bool {
  78. let query = [
  79. KeychainSwiftConstants.klass : kSecClassGenericPassword,
  80. KeychainSwiftConstants.attrAccount : key ]
  81. let status: OSStatus = SecItemDelete(query as CFDictionaryRef)
  82. return status == noErr
  83. }
  84. /**
  85. Deletes all keychain items used by the app.
  86. - returns: True if the keychain items were successfully deleted.
  87. */
  88. public class func clear() -> Bool {
  89. let query = [ kSecClass as String : kSecClassGenericPassword ]
  90. let status: OSStatus = SecItemDelete(query as CFDictionaryRef)
  91. return status == noErr
  92. }
  93. }