Advertisement
Guest User

Untitled

a guest
Dec 28th, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. import UIKit
  2. import Security
  3.  
  4. class Keychain {
  5.  
  6. class func save(key: String, data: NSData) -> Bool {
  7. let query = [
  8. kSecClass as String : kSecClassGenericPassword as String,
  9. kSecAttrAccount as String : key,
  10. kSecValueData as String : data ]
  11.  
  12. SecItemDelete(query as CFDictionaryRef)
  13.  
  14. let status: OSStatus = SecItemAdd(query as CFDictionaryRef, nil)
  15.  
  16. return status == noErr
  17. }
  18.  
  19. class func load(key: String) -> NSData? {
  20. let query = [
  21. kSecClass as String : kSecClassGenericPassword,
  22. kSecAttrAccount as String : key,
  23. kSecReturnData as String : kCFBooleanTrue,
  24. kSecMatchLimit as String : kSecMatchLimitOne ]
  25.  
  26. var dataTypeRef :Unmanaged<AnyObject>?
  27.  
  28. let status: OSStatus = SecItemCopyMatching(query, &dataTypeRef)
  29.  
  30. if status == noErr {
  31. return (dataTypeRef!.takeRetainedValue() as NSData)
  32. } else {
  33. return nil
  34. }
  35. }
  36.  
  37. class func delete(key: String) -> Bool {
  38. let query = [
  39. kSecClass as String : kSecClassGenericPassword,
  40. kSecAttrAccount as String : key ]
  41.  
  42. let status: OSStatus = SecItemDelete(query as CFDictionaryRef)
  43.  
  44. return status == noErr
  45. }
  46.  
  47.  
  48. class func clear() -> Bool {
  49. let query = [ kSecClass as String : kSecClassGenericPassword ]
  50.  
  51. let status: OSStatus = SecItemDelete(query as CFDictionaryRef)
  52.  
  53. return status == noErr
  54. }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement