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,
1212import UIKit
1313import 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
2828public 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: -
9588extension 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: -
167150extension 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: -
199175extension 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- //-----------------------------------------------------------------------------------------------------------------------------------------------
260221class 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}
0 commit comments