-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathPasscodeKit.swift
More file actions
executable file
·296 lines (246 loc) · 9.48 KB
/
Copy pathPasscodeKit.swift
File metadata and controls
executable file
·296 lines (246 loc) · 9.48 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//
// 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 CryptoKit
// MARK: - PasscodeKitDelegate
@objc public protocol PasscodeKitDelegate {
@objc optional func passcodeCreated(_ passcode: String)
@objc optional func passcodeChanged(_ passcode: String)
@objc optional func passcodeRemoved()
@objc optional func passcodeCheckedButDisabled()
@objc optional func passcodeEnteredSuccessfully()
@objc optional func passcodeClearedWhileVerifying()
@objc optional func passcodeMaximumFailedAttempts()
}
// MARK: - PasscodeKit
public class PasscodeKit: NSObject {
static let shared: PasscodeKit = {
let instance = PasscodeKit()
return instance
}()
public static var passcodeLength = 4
public static var allowedFailedAttempts = 3
public static var hapticFeedback = true
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 Face ID or Touch ID to unlock the app"
public static var delegate: PasscodeKitDelegate?
private var isStarted = false
public override init() {
super.init()
if #available(iOS 13.0, *) {
PasscodeKit.textColor = UIColor.label
PasscodeKit.backgroundColor = UIColor.systemGroupedBackground
} else {
PasscodeKit.backgroundColor = UIColor.groupTableViewBackground
}
}
public class func start() {
shared.start()
}
public class func dismiss() {
if (PasscodeKit.enabled()) {
if let navigationController = shared.topViewController() as? UINavigationController {
if let presentedView = navigationController.viewControllers.first {
if (presentedView is PasscodeKitVerify) {
presentedView.dismiss(animated: true)
}
}
}
}
}
}
// MARK: -
extension PasscodeKit {
private func start() {
guard !isStarted else { return }
isStarted = true
let didFinishLaunching = UIApplication.didFinishLaunchingNotification
let willEnterForeground = UIApplication.willEnterForegroundNotification
NotificationCenter.default.addObserver(self, selector: #selector(verifyPasscode), name: didFinishLaunching, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(verifyPasscode), name: willEnterForeground, object: nil)
}
@objc private func verifyPasscode() {
if (PasscodeKit.enabled()) {
if let viewController = topViewController() {
if (noPasscodePresented(viewController)) {
presentPasscodeVerify(viewController)
}
}
} else {
PasscodeKit.delegate?.passcodeCheckedButDisabled?()
}
}
private func presentPasscodeVerify(_ viewController: UIViewController) {
DispatchQueue.main.async {
let passcodeKitVerify = PasscodeKitVerify()
passcodeKitVerify.delegate = PasscodeKit.delegate
let navController = PasscodeKitNavController(rootViewController: passcodeKitVerify)
viewController.present(navController, animated: false)
}
}
private func noPasscodePresented(_ viewController: UIViewController) -> Bool {
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 }
}
}
return result
}
private func topViewController() -> UIViewController? {
let windowScene = UIApplication.shared.connectedScenes
.compactMap { $0 as? UIWindowScene }
.first { $0.activationState == .foregroundActive }
?? UIApplication.shared.connectedScenes
.compactMap { $0 as? UIWindowScene }
.first
let keyWindow = windowScene?.windows.first { $0.isKeyWindow }
?? windowScene?.windows.first
var viewController = keyWindow?.rootViewController
while (viewController?.presentedViewController != nil) {
viewController = viewController?.presentedViewController
}
return viewController
}
private func dismissVerifyIfNeeded() {
DispatchQueue.main.async {
if let navigationController = self.topViewController() as? UINavigationController {
if let presentedView = navigationController.viewControllers.first {
if (presentedView is PasscodeKitVerify) {
PasscodeKit.delegate?.passcodeClearedWhileVerifying?()
presentedView.dismiss(animated: true)
}
}
}
}
}
}
// MARK: -
extension PasscodeKit {
public class func createPasscode(_ viewController: UIViewController) {
let passcodeKitCreate = PasscodeKitCreate()
passcodeKitCreate.delegate = viewController as? PasscodeKitDelegate
let navController = PasscodeKitNavController(rootViewController: passcodeKitCreate)
viewController.present(navController, animated: true)
}
public class func changePasscode(_ viewController: UIViewController) {
let passcodeKitChange = PasscodeKitChange()
passcodeKitChange.delegate = viewController as? PasscodeKitDelegate
let navController = PasscodeKitNavController(rootViewController: passcodeKitChange)
viewController.present(navController, animated: true)
}
public class func removePasscode(_ viewController: UIViewController) {
let passcodeKitRemove = PasscodeKitRemove()
passcodeKitRemove.delegate = viewController as? PasscodeKitDelegate
let navController = PasscodeKitNavController(rootViewController: passcodeKitRemove)
viewController.present(navController, animated: true)
}
}
// MARK: -
extension PasscodeKit {
class func hapticAccept() {
guard hapticFeedback else { return }
UINotificationFeedbackGenerator().notificationOccurred(.success)
}
class func hapticDecline() {
guard hapticFeedback else { return }
UINotificationFeedbackGenerator().notificationOccurred(.error)
}
}
// MARK: -
extension PasscodeKit {
public class func enabled() -> Bool {
return (UserDefaults.standard.string(forKey: "PasscodeValue") != nil)
}
public class func verify(_ passcode: String) -> Bool {
if (passcode != "") {
return (UserDefaults.standard.string(forKey: "PasscodeValue") == sha256(passcode))
}
return (UserDefaults.standard.string(forKey: "PasscodeValue") == nil)
}
public class func update(_ passcode: String) {
if (passcode != "") {
UserDefaults.standard.set(sha256(passcode), forKey: "PasscodeValue")
} else {
UserDefaults.standard.removeObject(forKey: "PasscodeValue")
UserDefaults.standard.removeObject(forKey: "PasscodeBiometric")
shared.dismissVerifyIfNeeded()
}
}
public class func remove() {
UserDefaults.standard.removeObject(forKey: "PasscodeValue")
UserDefaults.standard.removeObject(forKey: "PasscodeBiometric")
shared.dismissVerifyIfNeeded()
}
public class func biometric() -> Bool {
return UserDefaults.standard.bool(forKey: "PasscodeBiometric")
}
public class func biometric(_ value: Bool) {
UserDefaults.standard.set(value, forKey: "PasscodeBiometric")
}
private class func sha256(_ text: String) -> String {
if #available(iOS 13.0, *) {
let data = Data(text.utf8)
let hash = SHA256.hash(data: data)
return hash.compactMap { String(format: "%02x", $0) }.joined()
}
return text
}
}
// MARK: - PasscodeKitNavController
class PasscodeKitNavController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
if #available(iOS 13.0, *) {
self.isModalInPresentation = true
self.modalPresentationStyle = .fullScreen
}
navigationBar.isTranslucent = false
navigationBar.tintColor = .systemBlue
navigationBar.barTintColor = PasscodeKit.backgroundColor
navigationBar.titleTextAttributes = [.foregroundColor: PasscodeKit.textColor]
if #available(iOS 15.0, *) {
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = PasscodeKit.backgroundColor
appearance.titleTextAttributes = [.foregroundColor: PasscodeKit.textColor]
navigationBar.standardAppearance = appearance
navigationBar.scrollEdgeAppearance = appearance
navigationBar.compactAppearance = appearance
}
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .portrait
}
override var shouldAutorotate: Bool {
return false
}
}