-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathPasscodeKitVerify.swift
More file actions
executable file
·169 lines (139 loc) · 4.96 KB
/
Copy pathPasscodeKitVerify.swift
File metadata and controls
executable file
·169 lines (139 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//
// Copyright (c) 2026 Related Code - https://relatedcode.com
//
// 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.
import UIKit
import LocalAuthentication
// MARK: - PasscodeKitVerify
class PasscodeKitVerify: UIViewController {
private var failedAttempts = 0
private var didSetupUI = false
private var viewPasscode = UIView()
private var textPasscode: PasscodeKitText!
private var labelInfo = UILabel()
private var labelFailedAttempts = UILabel()
private var keypad = PasscodeKitKeypad()
var delegate: PasscodeKitDelegate?
override func viewDidLoad() {
super.viewDidLoad()
title = PasscodeKit.titleEnterPasscode
view.backgroundColor = PasscodeKit.backgroundColor
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
authenticateBiometric()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
guard didSetupUI else { return }
keypad.layoutContentView(viewPasscode, in: view)
let width = viewPasscode.bounds.width
labelInfo.frame.size.width = width
textPasscode.frame.size.width = width
labelFailedAttempts.frame.origin.x = (width - labelFailedAttempts.frame.width) / 2
}
}
// MARK: -
extension PasscodeKitVerify {
private func authenticateBiometric() {
var error: NSError?
let context = LAContext()
if (PasscodeKit.biometric()) && (context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error)) {
let reason = PasscodeKit.textTouchIDAccessReason
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { success, error in
DispatchQueue.main.async {
self.actionBiometric(success)
}
}
} else {
actionBiometric(false)
}
}
private func actionBiometric(_ success: Bool) {
if (success) {
PasscodeKit.hapticAccept()
delegate?.passcodeEnteredSuccessfully?()
dismiss(animated: true)
} else {
setupUI()
updateUI()
}
}
}
// MARK: -
extension PasscodeKitVerify {
private func setupUI() {
if (didSetupUI) { return }
didSetupUI = true
view.backgroundColor = PasscodeKit.backgroundColor
viewPasscode.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 120)
view.addSubview(viewPasscode)
labelInfo.textAlignment = .center
labelInfo.textColor = PasscodeKit.textColor
labelInfo.font = UIFont.systemFont(ofSize: 17)
labelInfo.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 30)
viewPasscode.addSubview(labelInfo)
textPasscode = PasscodeKitText()
textPasscode.frame = CGRect(x: 0, y: 45, width: UIScreen.main.bounds.width, height: 30)
viewPasscode.addSubview(textPasscode)
labelFailedAttempts.textAlignment = .center
labelFailedAttempts.textColor = PasscodeKit.failedTextColor
labelFailedAttempts.backgroundColor = PasscodeKit.failedBackgroundColor
labelFailedAttempts.font = UIFont.systemFont(ofSize: 15)
labelFailedAttempts.frame = CGRect(x: (UIScreen.main.bounds.width - 225) / 2, y: 90, width: 225, height: 30)
labelFailedAttempts.layer.cornerRadius = 15
labelFailedAttempts.isHidden = true
labelFailedAttempts.clipsToBounds = true
viewPasscode.addSubview(labelFailedAttempts)
keypad.pinToBottom(of: view)
keypad.attach(to: textPasscode) { [weak self] current in
self?.actionPasscode(current)
}
}
private func updateUI() {
labelInfo.text = PasscodeKit.textEnterPasscode
failedAttempts = 0
textPasscode.text = ""
textPasscode.setNeedsLayout()
labelFailedAttempts.isHidden = true
}
private func setupUIFailed() {
PasscodeKit.hapticDecline()
let animation = CABasicAnimation(keyPath: "position")
animation.duration = 0.09
animation.repeatCount = 2
animation.isRemovedOnCompletion = true
animation.autoreverses = true
animation.fromValue = CGPoint(x: textPasscode.center.x - 10, y: textPasscode.center.y)
animation.toValue = CGPoint(x: textPasscode.center.x + 10, y: textPasscode.center.y)
textPasscode.layer.add(animation, forKey: "position")
failedAttempts += 1
labelFailedAttempts.isHidden = false
labelFailedAttempts.text = String(format: PasscodeKit.textFailedPasscode, failedAttempts)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
self.textPasscode.text = ""
self.textPasscode.setNeedsLayout()
}
if (failedAttempts >= PasscodeKit.allowedFailedAttempts) {
delegate?.passcodeMaximumFailedAttempts?()
}
}
}
// MARK: -
extension PasscodeKitVerify {
private func actionPasscode(_ current: String) {
if (PasscodeKit.verify(current)) {
PasscodeKit.hapticAccept()
delegate?.passcodeEnteredSuccessfully?()
dismiss(animated: true)
} else {
setupUIFailed()
}
}
}