section-1.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import UIKit
  2. import XCPlayground
  3. import KeychainAccess
  4. var keychain: Keychain
  5. /***************
  6. * Instantiation
  7. ***************/
  8. /* for Application Password */
  9. keychain = Keychain(service: "com.example.github-token")
  10. /* for Internet Password */
  11. let URL = NSURL(string: "https://github.com")!
  12. keychain = Keychain(server: URL, protocolType: .HTTPS)
  13. /**************
  14. * Adding items
  15. **************/
  16. /* subscripting */
  17. keychain["kishikawakatsumi"] = "01234567-89ab-cdef-0123-456789abcdef"
  18. /* set method */
  19. keychain.set("01234567-89ab-cdef-0123-456789abcdef", key: "kishikawakatsumi")
  20. /*****************
  21. * Obtaining items
  22. *****************/
  23. var token: String?
  24. /* subscripting (automatically convert to String) */
  25. token = keychain["kishikawakatsumi"]
  26. /* get method */
  27. // as String
  28. token = keychain.get("kishikawakatsumi")
  29. token = keychain.getString("kishikawakatsumi")
  30. // as Data
  31. let data = keychain.getData("kishikawakatsumi")
  32. /****************
  33. * Removing items
  34. ****************/
  35. /* subscripting */
  36. keychain["kishikawakatsumi"] = nil
  37. /* remove method */
  38. keychain.remove("kishikawakatsumi")
  39. /****************
  40. * Error handling
  41. *****************/
  42. /* set */
  43. if let error = keychain.set("01234567-89ab-cdef-0123-456789abcdef", key: "kishikawakatsumi") {
  44. println("error: \(error.localizedDescription)")
  45. }
  46. /* get */
  47. // First, get the failable (value or error) object
  48. let failable = keychain.getStringOrError("kishikawakatsumi")
  49. // 1. check the enum state
  50. switch failable {
  51. case .Success:
  52. println("token: \(failable.value)")
  53. case .Failure:
  54. println("error: \(failable.error)")
  55. }
  56. // 2. check the error object
  57. if let error = failable.error {
  58. println("error: \(failable.error)")
  59. } else {
  60. println("token: \(failable.value)")
  61. }
  62. // 3. check the failed property
  63. if failable.failed {
  64. println("error: \(failable.error)")
  65. } else {
  66. println("token: \(failable.value)")
  67. }
  68. /* remove */
  69. if let error = keychain.remove("kishikawakatsumi") {
  70. println("error: \(error.localizedDescription)")
  71. }
  72. /*******************
  73. * Label and Comment
  74. *******************/
  75. keychain = Keychain(server: NSURL(string: "https://github.com")!, protocolType: .HTTPS)
  76. .label("github.com (kishikawakatsumi)")
  77. .comment("github access token")
  78. /***************
  79. * Configuration
  80. ***************/
  81. /* for background application */
  82. let background = Keychain(service: "com.example.github-token")
  83. .accessibility(.AfterFirstUnlock)
  84. /* for forground application */
  85. let forground = Keychain(service: "com.example.github-token")
  86. .accessibility(.WhenUnlocked)
  87. /* Sharing Keychain Items */
  88. let shared = Keychain(service: "com.example.github-token", accessGroup: "12ABCD3E4F.shared")
  89. /* Synchronizing Keychain items with iCloud */
  90. let iCloud = Keychain(service: "com.example.github-token")
  91. .synchronizable(true)
  92. /* One-Shot configuration change */
  93. keychain
  94. .accessibility(.AfterFirstUnlock)
  95. .synchronizable(true)
  96. .set("01234567-89ab-cdef-0123-456789abcdef", key: "kishikawakatsumi")
  97. keychain
  98. .accessibility(.WhenUnlocked)
  99. .set("01234567-89ab-cdef-0123-456789abcdef", key: "kishikawakatsumi")
  100. /***********
  101. * Debugging
  102. ***********/
  103. /* Display all stored items if print keychain object */
  104. keychain = Keychain(server: NSURL(string: "https://github.com")!, protocolType: .HTTPS)
  105. println("\(keychain)")
  106. /* Obtaining all stored keys */
  107. let keys = keychain.allKeys()
  108. for key in keys {
  109. println("key: \(key)")
  110. }
  111. /* Obtaining all stored items */
  112. let items = keychain.allItems()
  113. for item in items {
  114. println("item: \(item)")
  115. }