ViewController.swift 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //
  2. // ViewController.swift
  3. // macOS Demo
  4. //
  5. // Created by Evgenii on 20/07/2016.
  6. // Copyright © 2016 Marketplacer. All rights reserved.
  7. //
  8. import Cocoa
  9. import KeychainSwift
  10. let TegKeychainDemo_keyName = "my key"
  11. class ViewController: NSViewController {
  12. @IBOutlet weak var textField: NSTextField!
  13. @IBOutlet weak var valueLabel: NSTextField!
  14. @IBOutlet weak var errorLabel: NSTextField!
  15. @IBOutlet weak var synchronizableButton: NSButton!
  16. let keychain = KeychainSwift()
  17. override func viewDidLoad() {
  18. super.viewDidLoad()
  19. updateValueLabel()
  20. errorLabel.stringValue = ""
  21. }
  22. // override var representedObject: Any? {
  23. // didSet {
  24. // // Update the view, if already loaded.
  25. // }
  26. // }
  27. @IBAction func onSaveTapped(_ sender: AnyObject) {
  28. keychain.synchronizable = synchronizableButton.state == NSControl.StateValue.on
  29. keychain.set(textField.stringValue, forKey: TegKeychainDemo_keyName)
  30. errorLabel.stringValue = "Result code: \(keychain.lastResultCode)"
  31. updateValueLabel()
  32. }
  33. @IBAction func onDeleteTapped(_ sender: AnyObject) {
  34. keychain.synchronizable = synchronizableButton.state == NSControl.StateValue.on
  35. keychain.delete(TegKeychainDemo_keyName)
  36. errorLabel.stringValue = "Result code: \(keychain.lastResultCode)"
  37. updateValueLabel()
  38. }
  39. @IBAction func onGetTapped(_ sender: AnyObject) {
  40. updateValueLabel()
  41. }
  42. private func updateValueLabel() {
  43. keychain.synchronizable = synchronizableButton.state == NSControl.StateValue.on
  44. if let value = keychain.get(TegKeychainDemo_keyName) {
  45. valueLabel.stringValue = "In Keychain: \(value)"
  46. } else {
  47. valueLabel.stringValue = "no value in keychain"
  48. }
  49. }
  50. }