Skip to content

Commit 5b49e41

Browse files
committed
1.0.4
1 parent c3199dc commit 5b49e41

14 files changed

+96
-221
lines changed

PasscodeKit/Sources/PasscodeKit.swift

Lines changed: 6 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright (c) 2021 Related Code - https://relatedcode.com
2+
// Copyright (c) 2023 Related Code - https://relatedcode.com
33
//
44
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
55
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
@@ -12,7 +12,7 @@
1212
import UIKit
1313
import CryptoKit
1414

15-
//-----------------------------------------------------------------------------------------------------------------------------------------------
15+
// MARK: - PasscodeKitDelegate
1616
@objc public protocol PasscodeKitDelegate {
1717

1818
@objc optional func passcodeCreated(_ passcode: String)
@@ -24,9 +24,8 @@ import CryptoKit
2424
@objc optional func passcodeMaximumFailedAttempts()
2525
}
2626

27-
//-----------------------------------------------------------------------------------------------------------------------------------------------
27+
// MARK: - PasscodeKit
2828
public class PasscodeKit: NSObject {
29-
3029
static let shared: PasscodeKit = {
3130
let instance = PasscodeKit()
3231
return instance
@@ -57,9 +56,7 @@ public class PasscodeKit: NSObject {
5756

5857
public static var delegate: PasscodeKitDelegate?
5958

60-
//-------------------------------------------------------------------------------------------------------------------------------------------
6159
public override init() {
62-
6360
super.init()
6461

6562
if #available(iOS 13.0, *) {
@@ -70,15 +67,11 @@ public class PasscodeKit: NSObject {
7067
}
7168
}
7269

73-
//-------------------------------------------------------------------------------------------------------------------------------------------
7470
public class func start() {
75-
7671
shared.start()
7772
}
7873

79-
//-------------------------------------------------------------------------------------------------------------------------------------------
8074
public class func dismiss() {
81-
8275
if (PasscodeKit.enabled()) {
8376
if let navigationController = shared.topViewController() as? UINavigationController {
8477
if let presentedView = navigationController.viewControllers.first {
@@ -91,22 +84,18 @@ public class PasscodeKit: NSObject {
9184
}
9285
}
9386

94-
//-----------------------------------------------------------------------------------------------------------------------------------------------
87+
// MARK: -
9588
extension PasscodeKit {
9689

97-
//-------------------------------------------------------------------------------------------------------------------------------------------
9890
private func start() {
99-
10091
let didFinishLaunching = UIApplication.didFinishLaunchingNotification
10192
let willEnterForeground = UIApplication.willEnterForegroundNotification
10293

10394
NotificationCenter.default.addObserver(self, selector: #selector(verifyPasscode), name: didFinishLaunching, object: nil)
10495
NotificationCenter.default.addObserver(self, selector: #selector(verifyPasscode), name: willEnterForeground, object: nil)
10596
}
10697

107-
//-------------------------------------------------------------------------------------------------------------------------------------------
10898
@objc private func verifyPasscode() {
109-
11099
if (PasscodeKit.enabled()) {
111100
if let viewController = topViewController() {
112101
if (noPasscodePresented(viewController)) {
@@ -118,9 +107,7 @@ extension PasscodeKit {
118107
}
119108
}
120109

121-
//-------------------------------------------------------------------------------------------------------------------------------------------
122110
private func presentPasscodeVerify(_ viewController: UIViewController) {
123-
124111
DispatchQueue.main.async {
125112
let passcodeKitVerify = PasscodeKitVerify()
126113
passcodeKitVerify.delegate = PasscodeKit.delegate
@@ -129,9 +116,7 @@ extension PasscodeKit {
129116
}
130117
}
131118

132-
//-------------------------------------------------------------------------------------------------------------------------------------------
133119
private func noPasscodePresented(_ viewController: UIViewController) -> Bool {
134-
135120
var result = true
136121
if let navigationController = viewController as? UINavigationController {
137122
if let presentedView = navigationController.viewControllers.first {
@@ -144,9 +129,7 @@ extension PasscodeKit {
144129
return result
145130
}
146131

147-
//-------------------------------------------------------------------------------------------------------------------------------------------
148132
private func topViewController() -> UIViewController? {
149-
150133
var keyWindow: UIWindow?
151134

152135
if #available(iOS 13.0, *) {
@@ -163,59 +146,46 @@ extension PasscodeKit {
163146
}
164147
}
165148

166-
//-----------------------------------------------------------------------------------------------------------------------------------------------
149+
// MARK: -
167150
extension PasscodeKit {
168151

169-
//-------------------------------------------------------------------------------------------------------------------------------------------
170152
public class func createPasscode(_ viewController: UIViewController) {
171-
172153
let passcodeKitCreate = PasscodeKitCreate()
173154
passcodeKitCreate.delegate = viewController as? PasscodeKitDelegate
174155
let navController = PasscodeKitNavController(rootViewController: passcodeKitCreate)
175156
viewController.present(navController, animated: true)
176157
}
177158

178-
//-------------------------------------------------------------------------------------------------------------------------------------------
179159
public class func changePasscode(_ viewController: UIViewController) {
180-
181160
let passcodeKitChange = PasscodeKitChange()
182161
passcodeKitChange.delegate = viewController as? PasscodeKitDelegate
183162
let navController = PasscodeKitNavController(rootViewController: passcodeKitChange)
184163
viewController.present(navController, animated: true)
185164
}
186165

187-
//-------------------------------------------------------------------------------------------------------------------------------------------
188166
public class func removePasscode(_ viewController: UIViewController) {
189-
190167
let passcodeKitRemove = PasscodeKitRemove()
191168
passcodeKitRemove.delegate = viewController as? PasscodeKitDelegate
192169
let navController = PasscodeKitNavController(rootViewController: passcodeKitRemove)
193170
viewController.present(navController, animated: true)
194171
}
195172
}
196173

197-
// MARK: - Passcode methods
198-
//-----------------------------------------------------------------------------------------------------------------------------------------------
174+
// MARK: -
199175
extension PasscodeKit {
200176

201-
//-------------------------------------------------------------------------------------------------------------------------------------------
202177
public class func enabled() -> Bool {
203-
204178
return (UserDefaults.standard.string(forKey: "PasscodeValue") != nil)
205179
}
206180

207-
//-------------------------------------------------------------------------------------------------------------------------------------------
208181
public class func verify(_ passcode: String) -> Bool {
209-
210182
if (passcode != "") {
211183
return (UserDefaults.standard.string(forKey: "PasscodeValue") == sha256(passcode))
212184
}
213185
return (UserDefaults.standard.string(forKey: "PasscodeValue") == nil)
214186
}
215187

216-
//-------------------------------------------------------------------------------------------------------------------------------------------
217188
public class func update(_ passcode: String) {
218-
219189
if (passcode != "") {
220190
UserDefaults.standard.set(sha256(passcode), forKey: "PasscodeValue")
221191
} else {
@@ -224,28 +194,20 @@ extension PasscodeKit {
224194
}
225195
}
226196

227-
//-------------------------------------------------------------------------------------------------------------------------------------------
228197
public class func remove() {
229-
230198
UserDefaults.standard.removeObject(forKey: "PasscodeValue")
231199
UserDefaults.standard.removeObject(forKey: "PasscodeBiometric")
232200
}
233201

234-
//-------------------------------------------------------------------------------------------------------------------------------------------
235202
public class func biometric() -> Bool {
236-
237203
return UserDefaults.standard.bool(forKey: "PasscodeBiometric")
238204
}
239205

240-
//-------------------------------------------------------------------------------------------------------------------------------------------
241206
public class func biometric(_ value: Bool) {
242-
243207
UserDefaults.standard.set(value, forKey: "PasscodeBiometric")
244208
}
245209

246-
//-------------------------------------------------------------------------------------------------------------------------------------------
247210
private class func sha256(_ text: String) -> String {
248-
249211
if #available(iOS 13.0, *) {
250212
let data = Data(text.utf8)
251213
let hash = SHA256.hash(data: data)
@@ -256,12 +218,9 @@ extension PasscodeKit {
256218
}
257219

258220
// MARK: - PasscodeKitNavController
259-
//-----------------------------------------------------------------------------------------------------------------------------------------------
260221
class PasscodeKitNavController: UINavigationController {
261222

262-
//-------------------------------------------------------------------------------------------------------------------------------------------
263223
override func viewDidLoad() {
264-
265224
super.viewDidLoad()
266225

267226
if #available(iOS 13.0, *) {
@@ -272,21 +231,15 @@ class PasscodeKitNavController: UINavigationController {
272231
navigationBar.isTranslucent = false
273232
}
274233

275-
//-------------------------------------------------------------------------------------------------------------------------------------------
276234
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
277-
278235
return .portrait
279236
}
280237

281-
//-------------------------------------------------------------------------------------------------------------------------------------------
282238
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
283-
284239
return .portrait
285240
}
286241

287-
//-------------------------------------------------------------------------------------------------------------------------------------------
288242
override var shouldAutorotate: Bool {
289-
290243
return false
291244
}
292245
}

PasscodeKit/Sources/PasscodeKitChange.swift

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright (c) 2021 Related Code - https://relatedcode.com
2+
// Copyright (c) 2023 Related Code - https://relatedcode.com
33
//
44
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
55
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
@@ -11,7 +11,7 @@
1111

1212
import UIKit
1313

14-
//-----------------------------------------------------------------------------------------------------------------------------------------------
14+
// MARK: - PasscodeState
1515
private enum PasscodeState {
1616

1717
case change1
@@ -20,7 +20,7 @@ private enum PasscodeState {
2020
case complete
2121
}
2222

23-
//-----------------------------------------------------------------------------------------------------------------------------------------------
23+
// MARK: - PasscodeKitChange
2424
class PasscodeKitChange: UIViewController {
2525

2626
private var state: PasscodeState!
@@ -37,9 +37,7 @@ class PasscodeKitChange: UIViewController {
3737

3838
var delegate: PasscodeKitDelegate?
3939

40-
//-------------------------------------------------------------------------------------------------------------------------------------------
4140
override func viewDidLoad() {
42-
4341
super.viewDidLoad()
4442

4543
title = PasscodeKit.titleChangePasscode
@@ -50,26 +48,21 @@ class PasscodeKitChange: UIViewController {
5048
updateUI()
5149
}
5250

53-
//-------------------------------------------------------------------------------------------------------------------------------------------
5451
override func viewDidAppear(_ animated: Bool) {
55-
5652
super.viewDidAppear(animated)
53+
5754
textPasscode.becomeFirstResponder()
5855
}
5956

60-
//-------------------------------------------------------------------------------------------------------------------------------------------
6157
@objc private func actionCancel() {
62-
6358
dismiss(animated: true)
6459
}
6560
}
6661

67-
//-----------------------------------------------------------------------------------------------------------------------------------------------
62+
// MARK: -
6863
extension PasscodeKitChange {
6964

70-
//-------------------------------------------------------------------------------------------------------------------------------------------
7165
private func setupUI() {
72-
7366
state = .change1
7467

7568
view.backgroundColor = PasscodeKit.backgroundColor
@@ -110,9 +103,7 @@ extension PasscodeKitChange {
110103
viewPasscode.addSubview(labelFailedAttempts)
111104
}
112105

113-
//-------------------------------------------------------------------------------------------------------------------------------------------
114106
private func updateUI() {
115-
116107
if (state == .change1) { labelInfo.text = PasscodeKit.textEnterOldPasscode }
117108
if (state == .change2) { labelInfo.text = PasscodeKit.textEnterNewPasscode }
118109
if (state == .change3) { labelInfo.text = PasscodeKit.textVerifyNewPasscode }
@@ -129,19 +120,15 @@ extension PasscodeKitChange {
129120
animateViewPasscode()
130121
}
131122

132-
//-------------------------------------------------------------------------------------------------------------------------------------------
133123
private func animateViewPasscode() {
134-
135124
let originalXPos = viewPasscode.frame.origin.x
136125
viewPasscode.frame.origin.x = originalXPos + (isPasscodeMismatch ? -250 : 250)
137126
UIView.animate(withDuration: 0.15) {
138127
self.viewPasscode.frame.origin.x = originalXPos
139128
}
140129
}
141130

142-
//-------------------------------------------------------------------------------------------------------------------------------------------
143131
private func setupUIFailed() {
144-
145132
let animation = CABasicAnimation(keyPath: "position")
146133
animation.duration = 0.09
147134
animation.repeatCount = 2
@@ -165,12 +152,10 @@ extension PasscodeKitChange {
165152
}
166153
}
167154

168-
//-----------------------------------------------------------------------------------------------------------------------------------------------
155+
// MARK: -
169156
extension PasscodeKitChange {
170157

171-
//-------------------------------------------------------------------------------------------------------------------------------------------
172158
@objc private func textFieldDidChangeEditing(_ textField: UITextField) {
173-
174159
let current = textField.text ?? ""
175160

176161
if (current.count >= PasscodeKit.passcodeLength) {
@@ -180,9 +165,7 @@ extension PasscodeKitChange {
180165
}
181166
}
182167

183-
//-------------------------------------------------------------------------------------------------------------------------------------------
184168
private func actionPasscode(_ current: String) {
185-
186169
if (state == .change1) {
187170
actionVerify(current)
188171
} else if (state == .change2) {
@@ -192,9 +175,7 @@ extension PasscodeKitChange {
192175
}
193176
}
194177

195-
//-------------------------------------------------------------------------------------------------------------------------------------------
196178
private func actionVerify(_ current: String) {
197-
198179
if (PasscodeKit.verify(current)) {
199180
state = .change2
200181
updateUI()
@@ -203,17 +184,13 @@ extension PasscodeKitChange {
203184
}
204185
}
205186

206-
//-------------------------------------------------------------------------------------------------------------------------------------------
207187
private func actionChange(_ current: String) {
208-
209188
state = .change3
210189
passcode = current
211190
updateUI()
212191
}
213192

214-
//-------------------------------------------------------------------------------------------------------------------------------------------
215193
private func actionConfirm(_ current: String) {
216-
217194
if (passcode == current) {
218195
PasscodeKit.update(passcode)
219196
delegate?.passcodeChanged?(passcode)

0 commit comments

Comments
 (0)