add passcode and touch ID
This commit is contained in:
parent
b022612e83
commit
a31e9776fe
8 changed files with 133 additions and 8 deletions
|
|
@ -8,6 +8,7 @@
|
|||
|
||||
import UIKit
|
||||
import CoreData
|
||||
import PasscodeLock
|
||||
|
||||
@UIApplicationMain
|
||||
class AppDelegate: UIResponder, UIApplicationDelegate {
|
||||
|
|
@ -23,6 +24,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
|
|||
func applicationWillResignActive(_ application: UIApplication) {
|
||||
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
|
||||
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
|
||||
let passcodeEnterViewController = PasscodeLockViewController(state: .enter, configuration: Globals.shared.passcodeConfiguration)
|
||||
UIApplication.shared.keyWindow?.rootViewController?.present(passcodeEnterViewController, animated: true, completion: nil)
|
||||
}
|
||||
|
||||
func applicationDidEnterBackground(_ application: UIApplication) {
|
||||
|
|
|
|||
|
|
@ -25,6 +25,6 @@ extension DefaultsKeys {
|
|||
static let gitRepositorySSHPrivateKeyPassphrase = DefaultsKey<String?>("gitRepositorySSHPrivateKeyPassphrase")
|
||||
static let lastUpdatedTime = DefaultsKey<Date?>("lasteUpdatedTime")
|
||||
|
||||
static let isPasscodeOn = DefaultsKey<Bool>("isPasscodeOn")
|
||||
static let isTouchIDOn = DefaultsKey<Bool>("isTouchIDOn")
|
||||
static let passcodeKey = DefaultsKey<String?>("passcodeKey")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,5 +14,6 @@ class Globals {
|
|||
let secringPath = "\(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])/secring.gpg"
|
||||
let sshPublicKeyPath = URL(fileURLWithPath: "\(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])/ssh_key.pub")
|
||||
let sshPrivateKeyPath = URL(fileURLWithPath: "\(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])/ssh_key")
|
||||
var passcodeConfiguration = PasscodeLockConfiguration()
|
||||
private init() { }
|
||||
}
|
||||
|
|
|
|||
29
pass/PasscodeLockConfiguration.swift
Normal file
29
pass/PasscodeLockConfiguration.swift
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
//
|
||||
// PasscodeLockConfiguration.swift
|
||||
// pass
|
||||
//
|
||||
// Created by Mingshen Sun on 7/2/2017.
|
||||
// Copyright © 2017 Bob Sun. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import PasscodeLock
|
||||
|
||||
struct PasscodeLockConfiguration: PasscodeLockConfigurationType {
|
||||
|
||||
let repository: PasscodeRepositoryType
|
||||
let passcodeLength = 4
|
||||
var isTouchIDAllowed = false
|
||||
let shouldRequestTouchIDImmediately = true
|
||||
let maximumInccorectPasscodeAttempts = 3
|
||||
|
||||
init(repository: PasscodeRepositoryType) {
|
||||
|
||||
self.repository = repository
|
||||
}
|
||||
|
||||
init() {
|
||||
|
||||
self.repository = PasscodeLockRepository()
|
||||
}
|
||||
}
|
||||
42
pass/PasscodeLockRepository.swift
Normal file
42
pass/PasscodeLockRepository.swift
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
//
|
||||
// PasscodeRepository.swift
|
||||
// pass
|
||||
//
|
||||
// Created by Mingshen Sun on 7/2/2017.
|
||||
// Copyright © 2017 Bob Sun. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import PasscodeLock
|
||||
import SwiftyUserDefaults
|
||||
|
||||
public class PasscodeLockRepository: PasscodeRepositoryType {
|
||||
private let passcodeKey = "passcode.lock.passcode"
|
||||
|
||||
public var hasPasscode: Bool {
|
||||
|
||||
if passcode != nil {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
private var passcode: String? {
|
||||
return Defaults[.passcodeKey]
|
||||
}
|
||||
|
||||
public func save(passcode: String) {
|
||||
Defaults[.passcodeKey] = passcode
|
||||
print(passcode)
|
||||
}
|
||||
|
||||
public func check(passcode: String) -> Bool {
|
||||
return self.passcode == passcode
|
||||
}
|
||||
|
||||
public func delete() {
|
||||
Defaults[.passcodeKey] = nil
|
||||
print("delete")
|
||||
}
|
||||
}
|
||||
|
|
@ -10,9 +10,11 @@ import UIKit
|
|||
import SVProgressHUD
|
||||
import CoreData
|
||||
import SwiftyUserDefaults
|
||||
import PasscodeLock
|
||||
|
||||
class SettingsTableViewController: UITableViewController {
|
||||
|
||||
|
||||
let repository = PasscodeLockRepository()
|
||||
@IBOutlet weak var pgpKeyTableViewCell: UITableViewCell!
|
||||
@IBOutlet weak var touchIDTableViewCell: UITableViewCell!
|
||||
@IBOutlet weak var passcodeTableViewCell: UITableViewCell!
|
||||
|
|
@ -108,13 +110,27 @@ class SettingsTableViewController: UITableViewController {
|
|||
super.viewDidLoad()
|
||||
let touchIDSwitch = UISwitch(frame: CGRect.zero)
|
||||
touchIDTableViewCell.accessoryView = touchIDSwitch
|
||||
touchIDSwitch.isOn = false
|
||||
touchIDSwitch.addTarget(self, action: #selector(touchIDSwitchAction), for: UIControlEvents.valueChanged)
|
||||
if Defaults[.isTouchIDOn] {
|
||||
touchIDSwitch.isOn = true
|
||||
} else {
|
||||
touchIDSwitch.isOn = false
|
||||
}
|
||||
if repository.hasPasscode {
|
||||
self.passcodeTableViewCell.detailTextLabel?.text = "On"
|
||||
print(Defaults[.passcodeKey]!)
|
||||
} else {
|
||||
self.passcodeTableViewCell.detailTextLabel?.text = "Off"
|
||||
}
|
||||
}
|
||||
|
||||
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
||||
if tableView.cellForRow(at: indexPath) == passcodeTableViewCell {
|
||||
showPasscodeActionSheet()
|
||||
if Defaults[.passcodeKey] != nil{
|
||||
showPasscodeActionSheet()
|
||||
} else {
|
||||
setPasscodeLock()
|
||||
}
|
||||
tableView.deselectRow(at: indexPath, animated: true)
|
||||
}
|
||||
}
|
||||
|
|
@ -130,20 +146,40 @@ class SettingsTableViewController: UITableViewController {
|
|||
|
||||
func touchIDSwitchAction(uiSwitch: UISwitch) {
|
||||
if uiSwitch.isOn {
|
||||
print("UISwitch is ON")
|
||||
Globals.shared.passcodeConfiguration.isTouchIDAllowed = true
|
||||
} else {
|
||||
print("UISwitch is OFF")
|
||||
Globals.shared.passcodeConfiguration.isTouchIDAllowed = false
|
||||
}
|
||||
}
|
||||
|
||||
func showPasscodeActionSheet() {
|
||||
let passcodeChangeViewController = PasscodeLockViewController(state: .change, configuration: Globals.shared.passcodeConfiguration)
|
||||
let passcodeRemoveViewController = PasscodeLockViewController(state: .remove, configuration: Globals.shared.passcodeConfiguration)
|
||||
|
||||
let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
|
||||
let removePasscodeAction = UIAlertAction(title: "Remove Passcode", style: .destructive, handler: nil)
|
||||
let changePasscodeAction = UIAlertAction(title: "Change Passcode", style: .default, handler: nil)
|
||||
let removePasscodeAction = UIAlertAction(title: "Remove Passcode", style: .destructive) { [unowned self] _ in
|
||||
passcodeRemoveViewController.successCallback = { _ in
|
||||
self.passcodeTableViewCell.detailTextLabel?.text = "Off"
|
||||
}
|
||||
self.present(passcodeRemoveViewController, animated: true, completion: nil)
|
||||
}
|
||||
|
||||
let changePasscodeAction = UIAlertAction(title: "Change Passcode", style: .default) { [unowned self] _ in
|
||||
self.present(passcodeChangeViewController, animated: true, completion: nil)
|
||||
}
|
||||
|
||||
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
|
||||
optionMenu.addAction(removePasscodeAction)
|
||||
optionMenu.addAction(changePasscodeAction)
|
||||
optionMenu.addAction(cancelAction)
|
||||
self.present(optionMenu, animated: true, completion: nil)
|
||||
}
|
||||
|
||||
func setPasscodeLock() {
|
||||
let passcodeSetViewController = PasscodeLockViewController(state: .set, configuration: Globals.shared.passcodeConfiguration)
|
||||
passcodeSetViewController.successCallback = { _ in
|
||||
self.passcodeTableViewCell.detailTextLabel?.text = "On"
|
||||
}
|
||||
present(passcodeSetViewController, animated: true, completion: nil)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue