InputViewController.swift 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // InputViewController.swift
  3. // Example
  4. //
  5. // Created by kishikawa katsumi on 2014/12/26.
  6. // Copyright (c) 2014 kishikawa katsumi. All rights reserved.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. import UIKit
  26. import KeychainAccess
  27. class InputViewController: UITableViewController {
  28. @IBOutlet weak var saveButton: UIBarButtonItem!
  29. @IBOutlet weak var cancelButton: UIBarButtonItem!
  30. @IBOutlet weak var usernameField: UITextField!
  31. @IBOutlet weak var passwordField: UITextField!
  32. @IBOutlet weak var serviceField: UITextField!
  33. override func viewDidLoad() {
  34. super.viewDidLoad()
  35. tableView.rowHeight = 44.0
  36. tableView.estimatedRowHeight = 44.0
  37. }
  38. override func didReceiveMemoryWarning() {
  39. super.didReceiveMemoryWarning()
  40. }
  41. // MARK:
  42. @IBAction func cancelAction(sender: UIBarButtonItem) {
  43. dismiss(animated: true, completion: nil)
  44. }
  45. @IBAction func saveAction(sender: UIBarButtonItem) {
  46. let keychain: Keychain
  47. if let service = serviceField.text, !service.isEmpty {
  48. keychain = Keychain(service: service)
  49. } else {
  50. keychain = Keychain()
  51. }
  52. keychain[usernameField.text!] = passwordField.text
  53. dismiss(animated: true, completion: nil)
  54. }
  55. @IBAction func editingChanged(sender: UITextField) {
  56. switch (usernameField.text, passwordField.text) {
  57. case let (username?, password?):
  58. saveButton.isEnabled = !username.isEmpty && !password.isEmpty
  59. case (_?, nil):
  60. saveButton.isEnabled = false
  61. case (nil, _?):
  62. saveButton.isEnabled = false
  63. case (nil, nil):
  64. saveButton.isEnabled = false
  65. }
  66. }
  67. }