Advertisement
rayhanjanam

Biometrics

Jan 13th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.19 KB | None | 0 0
  1. //
  2. //  Biometrics.swift
  3. //  Local Auth Demo
  4. //
  5. //  Created by Rayhan Nabi on 1/12/19.
  6. //  Copyright © 2019 Rayhan Janam. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import LocalAuthentication
  11.  
  12. enum BiometryType {
  13.   case faceID
  14.   case touchID
  15.   case none
  16. }
  17.  
  18. protocol BiometricAuthentication {
  19.   func didAuthenticate()
  20.   func didFailAuthentication(error: Error)
  21. }
  22.  
  23. final class Biometrics {
  24.  
  25.   static func getInstance(usePasscode: Bool) -> GenericBiometrics {
  26.     if #available(iOS 11.0, *) {
  27.       return RegularBiometrics(usePasscode)
  28.     } else {
  29.       return FallbackBiometrics(usePasscode)
  30.     }
  31.   }
  32.  
  33. }
  34.  
  35. class GenericBiometrics {
  36.  
  37.   private var policy: LAPolicy
  38.  
  39.   let context: LAContext
  40.  
  41.   var isSensorAvailable: Bool {
  42.     return context.canEvaluatePolicy(policy,
  43.                                      error: nil)
  44.   }
  45.  
  46.   var biometryType: BiometryType {
  47.     return .none
  48.   }
  49.  
  50.   var delegate: BiometricAuthentication?
  51.  
  52.   init(_ usePasscode: Bool) {
  53.     context = LAContext()
  54.     policy = usePasscode ? .deviceOwnerAuthentication : .deviceOwnerAuthenticationWithBiometrics
  55.   }
  56.  
  57.   func authenticate(reason: String) {
  58.     context.evaluatePolicy(policy, localizedReason: reason) { (result, error) in
  59.       DispatchQueue.main.async {
  60.         self.handleAuthState(result: result, error: error)
  61.         self.context.invalidate()
  62.       }
  63.     }
  64.   }
  65.  
  66.   private func handleAuthState(result: Bool, error: Error?) {
  67.     if result {
  68.       delegate?.didAuthenticate()
  69.     } else {
  70.       guard let error = error else { return }
  71.       delegate?.didFailAuthentication(error: error)
  72.     }
  73.   }
  74.  
  75. }
  76.  
  77. @available(iOS 11.0, *)
  78. final class RegularBiometrics: GenericBiometrics {
  79.  
  80.   override var biometryType: BiometryType {
  81.     if isSensorAvailable {
  82.       switch context.biometryType {
  83.       case .faceID:
  84.         return .faceID
  85.       case .touchID:
  86.         return .touchID
  87.       case .none:
  88.         return .none
  89.       }
  90.     }
  91.    
  92.     return .none
  93.   }
  94.  
  95. }
  96.  
  97. final class FallbackBiometrics: GenericBiometrics {
  98.  
  99.   override var biometryType: BiometryType {
  100.     if isSensorAvailable {
  101.       return .touchID
  102.     }
  103.    
  104.     return .none
  105.   }
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement