2017-01-19 21:15:47 +08:00
|
|
|
//
|
|
|
|
|
// SettingsTableViewController.swift
|
|
|
|
|
// pass
|
|
|
|
|
//
|
|
|
|
|
// Created by Mingshen Sun on 18/1/2017.
|
|
|
|
|
// Copyright © 2017 Bob Sun. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
import SVProgressHUD
|
|
|
|
|
import CoreData
|
2017-06-13 11:42:49 +08:00
|
|
|
import passKit
|
2017-01-19 21:15:47 +08:00
|
|
|
|
2018-01-16 20:26:41 -08:00
|
|
|
class SettingsTableViewController: UITableViewController, UITabBarControllerDelegate {
|
2017-01-22 01:42:36 +08:00
|
|
|
@IBOutlet weak var pgpKeyTableViewCell: UITableViewCell!
|
2017-02-07 17:56:31 +08:00
|
|
|
@IBOutlet weak var passcodeTableViewCell: UITableViewCell!
|
2017-02-21 13:07:14 +08:00
|
|
|
@IBOutlet weak var passwordRepositoryTableViewCell: UITableViewCell!
|
2018-01-29 03:23:34 +08:00
|
|
|
var setPasscodeLockAlert: UIAlertController?
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-16 22:39:03 -07:00
|
|
|
let passwordStore = PasswordStore.shared
|
2019-09-08 23:00:46 +02:00
|
|
|
let keychain = AppKeychain.shared
|
2018-01-29 03:23:34 +08:00
|
|
|
var passcodeLock = PasscodeLock.shared
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2018-01-16 20:26:41 -08:00
|
|
|
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
|
|
|
|
|
navigationController?.popViewController(animated: true)
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-02-17 13:44:25 +08:00
|
|
|
@IBAction func savePGPKey(segue: UIStoryboardSegue) {
|
2017-02-09 21:45:31 +08:00
|
|
|
if let controller = segue.source as? PGPKeySettingTableViewController {
|
2018-11-13 23:54:53 +01:00
|
|
|
SharedDefaults[.pgpPrivateKeyURL] = URL(string: controller.pgpPrivateKeyURLTextField.text!.trimmed)
|
|
|
|
|
SharedDefaults[.pgpPublicKeyURL] = URL(string: controller.pgpPublicKeyURLTextField.text!.trimmed)
|
2017-06-13 11:42:49 +08:00
|
|
|
SharedDefaults[.pgpKeySource] = "url"
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-02-17 14:58:27 +08:00
|
|
|
SVProgressHUD.setDefaultMaskType(.black)
|
|
|
|
|
SVProgressHUD.setDefaultStyle(.light)
|
2019-01-14 20:57:45 +01:00
|
|
|
SVProgressHUD.show(withStatus: "FetchingPgpKey".localize())
|
2017-02-17 14:58:27 +08:00
|
|
|
DispatchQueue.global(qos: .userInitiated).async { [unowned self] in
|
|
|
|
|
do {
|
2019-09-08 23:00:46 +02:00
|
|
|
try KeyFileManager.PublicPgp.importKey(from: SharedDefaults[.pgpPublicKeyURL]!)
|
|
|
|
|
try KeyFileManager.PrivatePgp.importKey(from: SharedDefaults[.pgpPrivateKeyURL]!)
|
|
|
|
|
try PGPAgent.shared.initKeys()
|
2017-02-17 14:58:27 +08:00
|
|
|
DispatchQueue.main.async {
|
2019-10-01 01:19:41 +08:00
|
|
|
self.setPGPKeyTableViewCellDetailText()
|
2019-01-14 20:57:45 +01:00
|
|
|
SVProgressHUD.showSuccess(withStatus: "Success".localize())
|
2017-02-17 14:58:27 +08:00
|
|
|
SVProgressHUD.dismiss(withDelay: 1)
|
2019-01-14 20:57:45 +01:00
|
|
|
Utils.alert(title: "RememberToRemoveKey".localize(), message: "RememberToRemoveKeyFromServer.".localize(), controller: self, completion: nil)
|
2017-02-17 14:58:27 +08:00
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
DispatchQueue.main.async {
|
2019-01-14 20:57:45 +01:00
|
|
|
self.pgpKeyTableViewCell.detailTextLabel?.text = "NotSet".localize()
|
|
|
|
|
Utils.alert(title: "Error".localize(), message: error.localizedDescription, controller: self, completion: nil)
|
2017-01-22 01:42:36 +08:00
|
|
|
}
|
|
|
|
|
}
|
2017-02-17 14:58:27 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-02-17 13:44:25 +08:00
|
|
|
} else if let controller = segue.source as? PGPKeyArmorSettingTableViewController {
|
2017-06-13 11:42:49 +08:00
|
|
|
SharedDefaults[.pgpKeySource] = "armor"
|
2019-09-08 23:00:46 +02:00
|
|
|
|
2017-02-17 13:44:25 +08:00
|
|
|
SVProgressHUD.setDefaultMaskType(.black)
|
|
|
|
|
SVProgressHUD.setDefaultStyle(.light)
|
2019-01-14 20:57:45 +01:00
|
|
|
SVProgressHUD.show(withStatus: "FetchingPgpKey".localize())
|
2017-02-17 13:44:25 +08:00
|
|
|
DispatchQueue.global(qos: .userInitiated).async { [unowned self] in
|
|
|
|
|
do {
|
2019-09-08 23:00:46 +02:00
|
|
|
try KeyFileManager.PublicPgp.importKey(from: controller.armorPublicKeyTextView.text ?? "")
|
|
|
|
|
try KeyFileManager.PrivatePgp.importKey(from: controller.armorPrivateKeyTextView.text ?? "")
|
|
|
|
|
try PGPAgent.shared.initKeys()
|
2017-02-17 13:44:25 +08:00
|
|
|
DispatchQueue.main.async {
|
2019-10-01 01:19:41 +08:00
|
|
|
self.setPGPKeyTableViewCellDetailText()
|
2019-01-14 20:57:45 +01:00
|
|
|
SVProgressHUD.showSuccess(withStatus: "Success".localize())
|
2017-02-17 13:44:25 +08:00
|
|
|
SVProgressHUD.dismiss(withDelay: 1)
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
DispatchQueue.main.async {
|
2019-01-14 20:57:45 +01:00
|
|
|
self.pgpKeyTableViewCell.detailTextLabel?.text = "NotSet".localize()
|
|
|
|
|
Utils.alert(title: "Error".localize(), message: error.localizedDescription, controller: self, completion: nil)
|
2017-02-17 13:44:25 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-19 21:15:47 +08:00
|
|
|
}
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-06-07 21:11:01 +08:00
|
|
|
private func saveImportedPGPKey() {
|
|
|
|
|
// load keys
|
2017-06-13 11:42:49 +08:00
|
|
|
SharedDefaults[.pgpKeySource] = "file"
|
2017-06-07 21:11:01 +08:00
|
|
|
SVProgressHUD.setDefaultMaskType(.black)
|
|
|
|
|
SVProgressHUD.setDefaultStyle(.light)
|
2019-01-14 20:57:45 +01:00
|
|
|
SVProgressHUD.show(withStatus: "FetchingPgpKey".localize())
|
2017-06-07 21:11:01 +08:00
|
|
|
DispatchQueue.global(qos: .userInitiated).async { [unowned self] in
|
|
|
|
|
do {
|
2019-09-08 23:00:46 +02:00
|
|
|
try KeyFileManager.PublicPgp.importKeyFromFileSharing()
|
|
|
|
|
try KeyFileManager.PrivatePgp.importKeyFromFileSharing()
|
|
|
|
|
try PGPAgent.shared.initKeys()
|
2017-06-07 21:11:01 +08:00
|
|
|
DispatchQueue.main.async {
|
2019-10-01 01:19:41 +08:00
|
|
|
self.setPGPKeyTableViewCellDetailText()
|
2019-01-14 20:57:45 +01:00
|
|
|
SVProgressHUD.showSuccess(withStatus: "Imported".localize())
|
2017-06-07 21:11:01 +08:00
|
|
|
SVProgressHUD.dismiss(withDelay: 1)
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
DispatchQueue.main.async {
|
2019-01-14 20:57:45 +01:00
|
|
|
self.pgpKeyTableViewCell.detailTextLabel?.text = "NotSet".localize()
|
|
|
|
|
Utils.alert(title: "Error".localize(), message: error.localizedDescription, controller: self, completion: nil)
|
2017-06-07 21:11:01 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-02-21 13:07:14 +08:00
|
|
|
@IBAction func saveGitServerSetting(segue: UIStoryboardSegue) {
|
2017-06-13 11:42:49 +08:00
|
|
|
self.passwordRepositoryTableViewCell.detailTextLabel?.text = SharedDefaults[.gitURL]?.host
|
2017-02-21 13:07:14 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-05-26 09:25:36 -07:00
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
|
|
|
return super.tableView(tableView, numberOfRowsInSection: section)
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-19 21:15:47 +08:00
|
|
|
override func viewDidLoad() {
|
|
|
|
|
super.viewDidLoad()
|
2017-03-18 00:18:55 +08:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(SettingsTableViewController.actOnPasswordStoreErasedNotification), name: .passwordStoreErased, object: nil)
|
2017-06-13 11:42:49 +08:00
|
|
|
self.passwordRepositoryTableViewCell.detailTextLabel?.text = SharedDefaults[.gitURL]?.host
|
2017-02-26 20:59:27 +08:00
|
|
|
setPGPKeyTableViewCellDetailText()
|
|
|
|
|
setPasswordRepositoryTableViewCellDetailText()
|
2018-01-29 03:23:34 +08:00
|
|
|
setPasscodeLockCell()
|
2017-02-26 20:59:27 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2018-01-16 21:54:00 -08:00
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
|
super.viewWillAppear(true)
|
|
|
|
|
tabBarController!.delegate = self
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2018-01-29 03:23:34 +08:00
|
|
|
private func setPasscodeLockCell() {
|
|
|
|
|
if passcodeLock.hasPasscode {
|
2019-01-14 20:57:45 +01:00
|
|
|
self.passcodeTableViewCell.detailTextLabel?.text = "On".localize()
|
2017-02-26 20:59:27 +08:00
|
|
|
} else {
|
2019-01-14 20:57:45 +01:00
|
|
|
self.passcodeTableViewCell.detailTextLabel?.text = "Off".localize()
|
2017-02-26 20:59:27 +08:00
|
|
|
}
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-02-26 20:59:27 +08:00
|
|
|
private func setPGPKeyTableViewCellDetailText() {
|
2019-10-01 01:19:41 +08:00
|
|
|
pgpKeyTableViewCell.detailTextLabel?.text = try? PGPAgent.shared.getKeyId() ?? "NotSet".localize()
|
2017-02-26 20:59:27 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-02-26 20:59:27 +08:00
|
|
|
private func setPasswordRepositoryTableViewCellDetailText() {
|
2017-06-13 11:42:49 +08:00
|
|
|
if SharedDefaults[.gitURL] == nil {
|
2019-01-14 20:57:45 +01:00
|
|
|
passwordRepositoryTableViewCell.detailTextLabel?.text = "NotSet".localize()
|
2017-02-26 20:59:27 +08:00
|
|
|
} else {
|
2017-06-13 11:42:49 +08:00
|
|
|
passwordRepositoryTableViewCell.detailTextLabel?.text = SharedDefaults[.gitURL]!.host
|
2017-02-26 20:59:27 +08:00
|
|
|
}
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-09-23 16:29:03 +08:00
|
|
|
@objc func actOnPasswordStoreErasedNotification() {
|
2017-02-26 20:59:27 +08:00
|
|
|
setPGPKeyTableViewCellDetailText()
|
|
|
|
|
setPasswordRepositoryTableViewCellDetailText()
|
2018-01-29 03:23:34 +08:00
|
|
|
setPasscodeLockCell()
|
2017-02-09 11:31:49 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2019-11-17 20:29:22 -08:00
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
|
|
|
let cell = super.tableView(tableView, cellForRowAt: indexPath)
|
|
|
|
|
cell.textLabel?.font = UIFont.preferredFont(forTextStyle: .body)
|
|
|
|
|
cell.textLabel?.adjustsFontForContentSizeCategory = true
|
|
|
|
|
cell.detailTextLabel?.adjustsFontForContentSizeCategory = true
|
|
|
|
|
return cell
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-07 17:56:31 +08:00
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
2019-11-17 17:52:31 -08:00
|
|
|
let cell = tableView.cellForRow(at: indexPath)
|
|
|
|
|
|
|
|
|
|
if cell == passcodeTableViewCell {
|
2019-07-10 21:57:15 +02:00
|
|
|
if passcodeLock.hasPasscode {
|
2017-02-07 20:24:58 +08:00
|
|
|
showPasscodeActionSheet()
|
|
|
|
|
} else {
|
|
|
|
|
setPasscodeLock()
|
|
|
|
|
}
|
2019-11-17 17:52:31 -08:00
|
|
|
} else if cell == pgpKeyTableViewCell {
|
2017-02-17 13:44:25 +08:00
|
|
|
showPGPKeyActionSheet()
|
2017-02-07 17:56:31 +08:00
|
|
|
}
|
2017-02-17 13:44:25 +08:00
|
|
|
tableView.deselectRow(at: indexPath, animated: true)
|
2017-02-07 16:45:14 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-02-17 13:44:25 +08:00
|
|
|
func showPGPKeyActionSheet() {
|
|
|
|
|
let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
|
2019-01-14 20:57:45 +01:00
|
|
|
var urlActionTitle = "DownloadFromUrl".localize()
|
|
|
|
|
var armorActionTitle = "AsciiArmorEncryptedKey".localize()
|
|
|
|
|
var fileActionTitle = "ITunesFileSharing".localize()
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-06-13 11:42:49 +08:00
|
|
|
if SharedDefaults[.pgpKeySource] == "url" {
|
2017-02-17 13:44:25 +08:00
|
|
|
urlActionTitle = "✓ \(urlActionTitle)"
|
2017-06-13 11:42:49 +08:00
|
|
|
} else if SharedDefaults[.pgpKeySource] == "armor" {
|
2017-02-17 13:44:25 +08:00
|
|
|
armorActionTitle = "✓ \(armorActionTitle)"
|
2017-06-13 11:42:49 +08:00
|
|
|
} else if SharedDefaults[.pgpKeySource] == "file" {
|
2017-02-24 17:59:04 +03:00
|
|
|
fileActionTitle = "✓ \(fileActionTitle)"
|
2017-02-17 13:44:25 +08:00
|
|
|
}
|
|
|
|
|
let urlAction = UIAlertAction(title: urlActionTitle, style: .default) { _ in
|
|
|
|
|
self.performSegue(withIdentifier: "setPGPKeyByURLSegue", sender: self)
|
|
|
|
|
}
|
|
|
|
|
let armorAction = UIAlertAction(title: armorActionTitle, style: .default) { _ in
|
|
|
|
|
self.performSegue(withIdentifier: "setPGPKeyByASCIISegue", sender: self)
|
|
|
|
|
}
|
2019-01-14 20:57:45 +01:00
|
|
|
let cancelAction = UIAlertAction(title: "Cancel".localize(), style: .cancel, handler: nil)
|
2017-02-17 13:44:25 +08:00
|
|
|
optionMenu.addAction(urlAction)
|
|
|
|
|
optionMenu.addAction(armorAction)
|
2017-02-24 17:59:04 +03:00
|
|
|
|
2019-09-08 23:00:46 +02:00
|
|
|
if KeyFileManager.PublicPgp.doesKeyFileExist() && KeyFileManager.PrivatePgp.doesKeyFileExist() {
|
2019-01-14 20:57:45 +01:00
|
|
|
fileActionTitle.append(" (\("Import".localize()))")
|
2017-02-24 17:59:04 +03:00
|
|
|
let fileAction = UIAlertAction(title: fileActionTitle, style: .default) { _ in
|
2017-06-07 21:11:01 +08:00
|
|
|
// passphrase related
|
2019-05-01 17:49:27 +02:00
|
|
|
let savePassphraseAlert = UIAlertController(title: "Passphrase".localize(), message: "WantToSavePassphrase?".localize(), preferredStyle: UIAlertController.Style.alert)
|
2017-06-07 21:11:01 +08:00
|
|
|
// no
|
2019-05-01 17:49:27 +02:00
|
|
|
savePassphraseAlert.addAction(UIAlertAction(title: "No".localize(), style: UIAlertAction.Style.default) { _ in
|
2019-09-08 23:00:46 +02:00
|
|
|
self.keychain.removeContent(for: Globals.pgpKeyPassphrase)
|
2017-10-08 21:37:58 +08:00
|
|
|
SharedDefaults[.isRememberPGPPassphraseOn] = false
|
2017-06-07 21:11:01 +08:00
|
|
|
self.saveImportedPGPKey()
|
|
|
|
|
})
|
|
|
|
|
// yes
|
2019-05-01 17:49:27 +02:00
|
|
|
savePassphraseAlert.addAction(UIAlertAction(title: "Yes".localize(), style: UIAlertAction.Style.destructive) {_ in
|
2017-06-07 21:11:01 +08:00
|
|
|
// ask for the passphrase
|
2019-05-01 17:49:27 +02:00
|
|
|
let alert = UIAlertController(title: "Passphrase".localize(), message: "FillInPgpPassphrase.".localize(), preferredStyle: UIAlertController.Style.alert)
|
|
|
|
|
alert.addAction(UIAlertAction(title: "Ok".localize(), style: UIAlertAction.Style.default, handler: {_ in
|
2019-09-08 23:00:46 +02:00
|
|
|
self.keychain.add(string: alert.textFields?.first?.text, for: Globals.pgpKeyPassphrase)
|
2017-10-08 21:37:58 +08:00
|
|
|
SharedDefaults[.isRememberPGPPassphraseOn] = true
|
2017-06-07 21:11:01 +08:00
|
|
|
self.saveImportedPGPKey()
|
|
|
|
|
}))
|
|
|
|
|
alert.addTextField(configurationHandler: {(textField: UITextField!) in
|
|
|
|
|
textField.text = ""
|
|
|
|
|
textField.isSecureTextEntry = true
|
|
|
|
|
})
|
|
|
|
|
self.present(alert, animated: true, completion: nil)
|
|
|
|
|
})
|
|
|
|
|
self.present(savePassphraseAlert, animated: true, completion: nil)
|
2017-02-24 17:59:04 +03:00
|
|
|
}
|
2017-06-07 16:54:54 +08:00
|
|
|
optionMenu.addAction(fileAction)
|
|
|
|
|
} else {
|
2019-01-14 20:57:45 +01:00
|
|
|
fileActionTitle.append(" (\("Tips".localize()))")
|
2017-06-14 20:22:15 +08:00
|
|
|
let fileAction = UIAlertAction(title: fileActionTitle, style: .default) { _ in
|
2019-01-14 20:57:45 +01:00
|
|
|
let title = "Tips".localize()
|
2019-01-20 11:13:27 +01:00
|
|
|
let message = "PgpCopyPublicAndPrivateKeyToPass.".localize()
|
2017-06-07 16:54:54 +08:00
|
|
|
Utils.alert(title: title, message: message, controller: self)
|
|
|
|
|
}
|
2017-02-24 17:59:04 +03:00
|
|
|
optionMenu.addAction(fileAction)
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
|
|
|
|
|
2017-06-13 11:42:49 +08:00
|
|
|
if SharedDefaults[.pgpKeySource] != nil {
|
2019-01-14 20:57:45 +01:00
|
|
|
let deleteAction = UIAlertAction(title: "RemovePgpKeys".localize(), style: .destructive) { _ in
|
2019-09-08 23:00:46 +02:00
|
|
|
self.keychain.removeContent(for: PgpKey.PUBLIC.getKeychainKey())
|
|
|
|
|
self.keychain.removeContent(for: PgpKey.PRIVATE.getKeychainKey())
|
|
|
|
|
PGPAgent.shared.uninitKeys()
|
2019-01-14 20:57:45 +01:00
|
|
|
self.pgpKeyTableViewCell.detailTextLabel?.text = "NotSet".localize()
|
2017-02-17 13:44:25 +08:00
|
|
|
}
|
|
|
|
|
optionMenu.addAction(deleteAction)
|
|
|
|
|
}
|
|
|
|
|
optionMenu.addAction(cancelAction)
|
2017-02-22 18:18:59 +08:00
|
|
|
optionMenu.popoverPresentationController?.sourceView = pgpKeyTableViewCell
|
|
|
|
|
optionMenu.popoverPresentationController?.sourceRect = pgpKeyTableViewCell.bounds
|
2017-02-17 13:44:25 +08:00
|
|
|
self.present(optionMenu, animated: true, completion: nil)
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-02-07 17:56:31 +08:00
|
|
|
func showPasscodeActionSheet() {
|
|
|
|
|
let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
|
2018-01-29 03:23:34 +08:00
|
|
|
let passcodeRemoveViewController = PasscodeLockViewController()
|
2018-12-09 16:59:07 -08:00
|
|
|
|
|
|
|
|
|
2019-01-14 20:57:45 +01:00
|
|
|
let removePasscodeAction = UIAlertAction(title: "RemovePasscode".localize(), style: .destructive) { [weak self] _ in
|
2018-01-29 03:23:34 +08:00
|
|
|
passcodeRemoveViewController.successCallback = {
|
|
|
|
|
self?.passcodeLock.delete()
|
|
|
|
|
self?.setPasscodeLockCell()
|
2017-02-07 20:24:58 +08:00
|
|
|
}
|
2017-02-10 00:56:12 +08:00
|
|
|
self?.present(passcodeRemoveViewController, animated: true, completion: nil)
|
2017-02-07 20:24:58 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2019-01-14 20:57:45 +01:00
|
|
|
let changePasscodeAction = UIAlertAction(title: "ChangePasscode".localize(), style: .default) { [weak self] _ in
|
2018-01-29 03:23:34 +08:00
|
|
|
self?.setPasscodeLock()
|
2017-02-07 20:24:58 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2019-01-14 20:57:45 +01:00
|
|
|
let cancelAction = UIAlertAction(title: "Cancel".localize(), style: .cancel, handler: nil)
|
2017-02-07 17:56:31 +08:00
|
|
|
optionMenu.addAction(removePasscodeAction)
|
|
|
|
|
optionMenu.addAction(changePasscodeAction)
|
|
|
|
|
optionMenu.addAction(cancelAction)
|
2017-02-22 18:18:59 +08:00
|
|
|
optionMenu.popoverPresentationController?.sourceView = passcodeTableViewCell
|
|
|
|
|
optionMenu.popoverPresentationController?.sourceRect = passcodeTableViewCell.bounds
|
2017-02-07 17:56:31 +08:00
|
|
|
self.present(optionMenu, animated: true, completion: nil)
|
|
|
|
|
}
|
2018-01-29 03:23:34 +08:00
|
|
|
|
|
|
|
|
@objc func alertTextFieldDidChange(_ sender: UITextField) {
|
|
|
|
|
// check whether we should enable the Save button in setPasscodeLockAlert
|
|
|
|
|
if let setPasscodeLockAlert = self.setPasscodeLockAlert,
|
|
|
|
|
let setPasscodeLockAlertTextFields0 = setPasscodeLockAlert.textFields?[0],
|
|
|
|
|
let setPasscodeLockAlertTextFields1 = setPasscodeLockAlert.textFields?[1] {
|
|
|
|
|
if sender == setPasscodeLockAlertTextFields0 || sender == setPasscodeLockAlertTextFields1 {
|
|
|
|
|
// two passwords should be the same, and length >= 4
|
|
|
|
|
let passcodeText = setPasscodeLockAlertTextFields0.text!
|
|
|
|
|
let passcodeConfirmationText = setPasscodeLockAlertTextFields1.text!
|
|
|
|
|
setPasscodeLockAlert.actions[0].isEnabled = passcodeText == passcodeConfirmationText && passcodeText.count >= 4
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-02-07 20:24:58 +08:00
|
|
|
func setPasscodeLock() {
|
2018-01-29 03:23:34 +08:00
|
|
|
// prepare the alert for setting the passcode
|
2019-01-14 20:57:45 +01:00
|
|
|
setPasscodeLockAlert = UIAlertController(title: "SetPasscode".localize(), message: "FillInAppPasscode.".localize(), preferredStyle: .alert)
|
2018-01-29 03:23:34 +08:00
|
|
|
setPasscodeLockAlert?.addTextField(configurationHandler: {(_ textField: UITextField) -> Void in
|
2019-02-13 21:31:40 +01:00
|
|
|
textField.placeholder = "Passcode".localize()
|
2018-01-29 03:23:34 +08:00
|
|
|
textField.isSecureTextEntry = true
|
2019-05-01 17:49:27 +02:00
|
|
|
textField.addTarget(self, action: #selector(self.alertTextFieldDidChange(_:)), for: UIControl.Event.editingChanged)
|
2018-01-29 03:23:34 +08:00
|
|
|
})
|
|
|
|
|
setPasscodeLockAlert?.addTextField(configurationHandler: {(_ textField: UITextField) -> Void in
|
2019-01-14 20:57:45 +01:00
|
|
|
textField.placeholder = "PasswordConfirmation".localize()
|
2018-01-29 03:23:34 +08:00
|
|
|
textField.isSecureTextEntry = true
|
2019-05-01 17:49:27 +02:00
|
|
|
textField.addTarget(self, action: #selector(self.alertTextFieldDidChange(_:)), for: UIControl.Event.editingChanged)
|
2018-01-29 03:23:34 +08:00
|
|
|
})
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2018-01-29 03:23:34 +08:00
|
|
|
// save action
|
2019-01-14 20:57:45 +01:00
|
|
|
let saveAction = UIAlertAction(title: "Save".localize(), style: .default) { (action:UIAlertAction) -> Void in
|
2018-01-29 03:23:34 +08:00
|
|
|
let passcode: String = self.setPasscodeLockAlert!.textFields![0].text!
|
|
|
|
|
self.passcodeLock.save(passcode: passcode)
|
|
|
|
|
// refresh the passcode lock cell ("On")
|
|
|
|
|
self.setPasscodeLockCell()
|
2017-02-07 20:24:58 +08:00
|
|
|
}
|
2018-01-29 03:23:34 +08:00
|
|
|
saveAction.isEnabled = false // disable the Save button by default
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2018-01-29 03:23:34 +08:00
|
|
|
// cancel action
|
2019-01-14 20:57:45 +01:00
|
|
|
let cancelAction = UIAlertAction(title: "Cancel".localize(), style: .cancel, handler: nil)
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2018-01-29 03:23:34 +08:00
|
|
|
// present
|
|
|
|
|
setPasscodeLockAlert?.addAction(saveAction)
|
|
|
|
|
setPasscodeLockAlert?.addAction(cancelAction)
|
|
|
|
|
self.present(setPasscodeLockAlert!, animated: true, completion: nil)
|
2017-02-07 20:24:58 +08:00
|
|
|
}
|
2017-01-19 21:15:47 +08:00
|
|
|
}
|