section-1.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 = URL(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. try? 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 = try! keychain.get("kishikawakatsumi")
  29. token = try! keychain.getString("kishikawakatsumi")
  30. // as Data
  31. let data = try! keychain.getData("kishikawakatsumi")
  32. /****************
  33. * Removing items
  34. ****************/
  35. /* subscripting */
  36. keychain["kishikawakatsumi"] = nil
  37. /* remove method */
  38. try? keychain.remove("kishikawakatsumi")
  39. /****************
  40. * Error handling
  41. ****************/
  42. /* set */
  43. do {
  44. try keychain.set("01234567-89ab-cdef-0123-456789abcdef", key: "kishikawakatsumi")
  45. } catch let error {
  46. print("error: \(error.localizedDescription)")
  47. }
  48. /* get */
  49. // First, get the failable (value or error) object
  50. do {
  51. let token = try keychain.get("kishikawakatsumi")
  52. } catch let error {
  53. print("error: \(error.localizedDescription)")
  54. }
  55. /* remove */
  56. do {
  57. try keychain.remove("kishikawakatsumi")
  58. } catch let error {
  59. print("error: \(error.localizedDescription)")
  60. }
  61. /*******************
  62. * Label and Comment
  63. *******************/
  64. keychain = Keychain(server: URL(string: "https://github.com")!, protocolType: .https)
  65. .label("github.com (kishikawakatsumi)")
  66. .comment("github access token")
  67. /***************
  68. * Configuration
  69. ***************/
  70. /* for background application */
  71. let background = Keychain(service: "com.example.github-token")
  72. .accessibility(.afterFirstUnlock)
  73. /* for forground application */
  74. let forground = Keychain(service: "com.example.github-token")
  75. .accessibility(.whenUnlocked)
  76. /* Sharing Keychain Items */
  77. let shared = Keychain(service: "com.example.github-token", accessGroup: "12ABCD3E4F.shared")
  78. /* Synchronizing Keychain items with iCloud */
  79. let iCloud = Keychain(service: "com.example.github-token")
  80. .synchronizable(true)
  81. /* One-Shot configuration change */
  82. try? keychain
  83. .accessibility(.afterFirstUnlock)
  84. .synchronizable(true)
  85. .set("01234567-89ab-cdef-0123-456789abcdef", key: "kishikawakatsumi")
  86. try? keychain
  87. .accessibility(.whenUnlocked)
  88. .set("01234567-89ab-cdef-0123-456789abcdef", key: "kishikawakatsumi")
  89. /***********
  90. * Debugging
  91. ***********/
  92. /* Display all stored items if print keychain object */
  93. keychain = Keychain(server: URL(string: "https://github.com")!, protocolType: .https)
  94. print("\(keychain)")
  95. /* Obtaining all stored keys */
  96. let keys = keychain.allKeys()
  97. for key in keys {
  98. print("key: \(key)")
  99. }
  100. /* Obtaining all stored items */
  101. let items = keychain.allItems()
  102. for item in items {
  103. print("item: \(item)")
  104. }