Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
MIT License

Copyright (c) 2022 StephenFang

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Copyright (c) 2021 Related Code

Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
85 changes: 85 additions & 0 deletions PasscodeKit/Sources/PasscodeInterval.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//
// PasscodeInterval.swift
// app
//
// Created by StephenFang on 2022/7/16.
// Copyright © 2022 KZ. All rights reserved.
//

import Foundation

enum PasscodeInterval: Double, CaseIterable {
case immediately = 0.0
case oneMinute = 1.0
case fiveMinutes = 5.0
case tenMinutes = 10.0
case halfAnHour = 30.0
case anHour = 60.0

var localizedDescription: String {
if self == .immediately {
return PasscodeKit.vefifyPasscodeImmediately
} else if self == .anHour {
return PasscodeKit.vefifyPasscodeAfterOneHour
} else {
return String(format: PasscodeKit.vefifyPasscodeAfterMinutes, rawValue)
}
}
}

class PasscodeKitInterval: UIViewController {

fileprivate let tableView = UITableView(frame: .zero, style: .insetGrouped)
fileprivate var selectedRow = 0

override func viewDidLoad() {
super.viewDidLoad()

title = "Require Password"

navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: nil, action: nil)

view.addSubview(tableView)
tableView.frame = view.bounds
tableView.delegate = self
tableView.dataSource = self
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

if let selectedRow = PasscodeInterval.allCases.firstIndex(where: { $0.rawValue == PasscodeKit.passcodeInterval()
}) {
self.selectedRow = selectedRow
}
tableView.selectRow(at: IndexPath(item: selectedRow, section: 0), animated: false, scrollPosition: .top)
}
}

extension IntervalViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return PasscodeInterval.allCases.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

var cell: UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: "cell")
if (cell == nil) { cell = UITableViewCell(style: .default, reuseIdentifier: "cell") }

cell.selectionStyle = .none
cell.textLabel?.text = PasscodeInterval.allCases[indexPath.item].localizedDescription
cell.accessoryType = indexPath.row == selectedRow ? .checkmark : .none

return cell
}
}

extension IntervalViewController: UITableViewDelegate {

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
PasscodeKit.passcodeInterval(PasscodeInterval.allCases[indexPath.item].rawValue)

selectedRow = indexPath.row
tableView.reloadData()
}
}
127 changes: 99 additions & 28 deletions PasscodeKit/Sources/PasscodeKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import CryptoKit
@objc optional func passcodeCheckedButDisabled()
@objc optional func passcodeEnteredSuccessfully()
@objc optional func passcodeMaximumFailedAttempts()

@objc optional func passcodeIntervalChanged()
}

//-----------------------------------------------------------------------------------------------------------------------------------------------
Expand All @@ -32,29 +34,34 @@ public class PasscodeKit: NSObject {
return instance
}()

public static var passcodeLength = 4
public static var allowedFailedAttempts = 3

public static var textColor = UIColor.darkText
public static var backgroundColor = UIColor.lightGray

public static var failedTextColor = UIColor.white
public static var failedBackgroundColor = UIColor.systemRed

public static var titleEnterPasscode = "Enter Passcode"
public static var titleCreatePasscode = "Create Passcode"
public static var titleChangePasscode = "Change Passcode"
public static var titleRemovePasscode = "Remove Passcode"

public static var textEnterPasscode = "Enter your passcode"
public static var textVerifyPasscode = "Verify your passcode"
public static var textEnterOldPasscode = "Enter your old passcode"
public static var textEnterNewPasscode = "Enter your new passcode"
public static var textVerifyNewPasscode = "Verify your new passcode"
public static var textFailedPasscode = "%d Failed Passcode Attempts"
public static var textPasscodeMismatch = "Passcodes did not match. Try again."
public static var textTouchIDAccessReason = "Please use Touch ID to unlock the app"

public static var passcodeLength = 4
public static var allowedFailedAttempts = 3

public static var textColor = UIColor.darkText
public static var backgroundColor = UIColor.lightGray

public static var failedTextColor = UIColor.white
public static var failedBackgroundColor = UIColor.systemRed

public static var titleEnterPasscode = "Enter Passcode"
public static var titleCreatePasscode = "Create Passcode"
public static var titleChangePasscode = "Change Passcode"
public static var titleRemovePasscode = "Remove Passcode"

public static var textEnterPasscode = "Enter your passcode"
public static var textVerifyPasscode = "Verify your passcode"
public static var textEnterOldPasscode = "Enter your old passcode"
public static var textEnterNewPasscode = "Enter your new passcode"
public static var textVerifyNewPasscode = "Verify your new passcode"
public static var textFailedPasscode = "%d Failed Passcode Attempts"
public static var textPasscodeMismatch = "Passcodes did not match. Try again."
public static var textBiometricAccessReason = "Please use Face ID (or Touch ID) to unlock the app"
public static var textBiometricAccessTip = "Allow to use Face ID (or Touch ID) to unlock the app."

public static var vefifyPasscodeImmediately = "Immediately"
public static var vefifyPasscodeAfterMinutes = "After %.f minutes"
public static var vefifyPasscodeAfterOneHour = "After an hour"

public static var delegate: PasscodeKitDelegate?

//-------------------------------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -99,15 +106,20 @@ extension PasscodeKit {

let didFinishLaunching = UIApplication.didFinishLaunchingNotification
let willEnterForeground = UIApplication.willEnterForegroundNotification
let willResignActive = UIApplication.willResignActiveNotification

NotificationCenter.default.addObserver(self, selector: #selector(verifyPasscode), name: didFinishLaunching, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(verifyPasscode), name: willEnterForeground, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(verifyInterval), name: willResignActive, object: nil)
}

//-------------------------------------------------------------------------------------------------------------------------------------------
@objc private func verifyPasscode() {

if (PasscodeKit.enabled()) {
if (!PasscodeKit.verifiedTimeExpired()) {
return
}
if let viewController = topViewController() {
if (noPasscodePresented(viewController)) {
presentPasscodeVerify(viewController)
Expand All @@ -117,6 +129,13 @@ extension PasscodeKit {
PasscodeKit.delegate?.passcodeCheckedButDisabled?()
}
}

//-------------------------------------------------------------------------------------------------------------------------------------------
@objc private func verifyInterval() {
if (PasscodeKit.enabled()) {
PasscodeKit.verifiedTimeInterval(Date())
}
}

//-------------------------------------------------------------------------------------------------------------------------------------------
private func presentPasscodeVerify(_ viewController: UIViewController) {
Expand All @@ -135,10 +154,10 @@ extension PasscodeKit {
var result = true
if let navigationController = viewController as? UINavigationController {
if let presentedView = navigationController.viewControllers.first {
if (presentedView is PasscodeKitCreate) { result = false }
if (presentedView is PasscodeKitChange) { result = false }
if (presentedView is PasscodeKitRemove) { result = false }
if (presentedView is PasscodeKitVerify) { result = false }
if (presentedView is PasscodeKitCreate) { result = false }
if (presentedView is PasscodeKitChange) { result = false }
if (presentedView is PasscodeKitRemove) { result = false }
if (presentedView is PasscodeKitVerify) { result = false }
}
}
return result
Expand Down Expand Up @@ -192,6 +211,14 @@ extension PasscodeKit {
let navController = PasscodeKitNavController(rootViewController: passcodeKitRemove)
viewController.present(navController, animated: true)
}

//-------------------------------------------------------------------------------------------------------------------------------------------
public class func changeInterval(_ viewController: UINavigationController) {

let passcodeKitInterval = PasscodeKitInterval()
passcodeKitInterval.delegate = viewController as? PasscodeKitDelegate
viewController.pushViewController(passcodeKitInterval, animated: true)
}
}

// MARK: - Passcode methods
Expand Down Expand Up @@ -229,6 +256,8 @@ extension PasscodeKit {

UserDefaults.standard.removeObject(forKey: "PasscodeValue")
UserDefaults.standard.removeObject(forKey: "PasscodeBiometric")
UserDefaults.standard.removeObject(forKey: "PasscodeInterval")
UserDefaults.standard.removeObject(forKey: "PasscodeVerifiedInterval")
}

//-------------------------------------------------------------------------------------------------------------------------------------------
Expand All @@ -253,6 +282,40 @@ extension PasscodeKit {
}
return text
}

//-------------------------------------------------------------------------------------------------------------------------------------------
public class func passcodeInterval() -> Double {
return UserDefaults.standard.double(forKey: "PasscodeInterval")
}

//-------------------------------------------------------------------------------------------------------------------------------------------
public class func passcodeInterval(_ interval: Double) {
UserDefaults.standard.set(interval, forKey: "PasscodeInterval")
}

//-------------------------------------------------------------------------------------------------------------------------------------------
public class func passcodeLocalizedInterval() -> String {
return PasscodeInterval.allCases.first(where: { $0.rawValue == PasscodeKit.passcodeInterval()})?.localizedDescription ?? PasscodeKit.vefifyPasscodeImmediately
}

//-------------------------------------------------------------------------------------------------------------------------------------------
public class func verifiedTimeInterval() -> Double {
return UserDefaults.standard.double(forKey: "PasscodeVerifiedInterval")
}


//-------------------------------------------------------------------------------------------------------------------------------------------
public class func verifiedTimeInterval(_ date: Date) {
UserDefaults.standard.set(date.timeIntervalSince1970, forKey: "PasscodeVerifiedInterval")
}

//-------------------------------------------------------------------------------------------------------------------------------------------
private class func verifiedTimeExpired() -> Bool {
let now = Date().timeIntervalSince1970
let prev = verifiedTimeInterval()
let seconds: Double = abs(now - prev)
return seconds >= passcodeInterval()
}
}

// MARK: - PasscodeKitNavController
Expand All @@ -269,7 +332,15 @@ class PasscodeKitNavController: UINavigationController {
self.modalPresentationStyle = .fullScreen
}

navigationBar.isTranslucent = false
if #available(iOS 15.0, *) {
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
UINavigationBar.appearance().compactAppearance = appearance
} else {
navigationBar.isTranslucent = false
}
}

//-------------------------------------------------------------------------------------------------------------------------------------------
Expand Down
Loading