ViewController.swift 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import UIKit
  2. import KeychainSwift
  3. let TegKeychainDemo_keyName = "my key"
  4. class ViewController: UIViewController {
  5. @IBOutlet weak var textField: UITextField!
  6. @IBOutlet weak var valueLabel: UILabel!
  7. @IBOutlet weak var synchronizableSwitch: UISwitch!
  8. let keychain = KeychainSwift()
  9. override func viewDidLoad() {
  10. super.viewDidLoad()
  11. updateValueLabel()
  12. }
  13. override func didReceiveMemoryWarning() {
  14. super.didReceiveMemoryWarning()
  15. // Dispose of any resources that can be recreated.
  16. }
  17. @IBAction func onSaveTapped(sender: AnyObject) {
  18. if let text = textField.text {
  19. keychain.synchronizable = synchronizableSwitch.on
  20. keychain.set(text, forKey: TegKeychainDemo_keyName)
  21. updateValueLabel()
  22. }
  23. }
  24. @IBAction func onDeleteTapped(sender: AnyObject) {
  25. keychain.synchronizable = synchronizableSwitch.on
  26. keychain.delete(TegKeychainDemo_keyName)
  27. updateValueLabel()
  28. }
  29. @IBAction func onGetTapped(sender: AnyObject) {
  30. updateValueLabel()
  31. }
  32. private func updateValueLabel() {
  33. keychain.synchronizable = synchronizableSwitch.on
  34. if let value = keychain.get(TegKeychainDemo_keyName) {
  35. valueLabel.text = "In Keychain: \(value)"
  36. } else {
  37. valueLabel.text = "no value in keychain"
  38. }
  39. }
  40. }