|
| 1 | +// |
| 2 | +// JMBiometricsPermissionManager.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Nevio Hirani on 20.01.24. |
| 6 | +// Github: N3v1 - |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | + |
| 11 | +import LocalAuthentication |
| 12 | +import CorePermissionsSwiftUI |
| 13 | + |
| 14 | +/// A permission manager for handling biometric authentication requests. |
| 15 | +@available(iOS 13.0, macOS 11.0, *) |
| 16 | +public extension PermissionManager { |
| 17 | + /// Shared instance for managing biometric permissions. |
| 18 | + static let opticBiometrics = JMBiometricPermissionManager() |
| 19 | +} |
| 20 | + |
| 21 | +/// A permission manager specifically designed for handling biometric authentication requests, such as Face ID, Touch ID and Optic ID. |
| 22 | +/// |
| 23 | +/// `JMBiometricPermissionManager` provides a streamlined interface for checking and requesting biometric authentication permissions. |
| 24 | +/// It encapsulates the complexities associated with the LocalAuthentication framework, making it easy to integrate biometric security |
| 25 | +/// features into your app. The class is part of the `CorePermissionsSwiftUI` framework and aligns with the standardized `PermissionManager` protocol. |
| 26 | +/// |
| 27 | +/// ## Usage |
| 28 | +/// To utilize biometric authentication in your application, follow the guide in the README.md |
| 29 | +@available(iOS 13.0, macOS 11.0, *) |
| 30 | +public final class JMBiometricPermissionManager: PermissionManager { |
| 31 | + |
| 32 | + public override var permissionType: PermissionType { |
| 33 | + .biometrics |
| 34 | + } |
| 35 | + |
| 36 | + /// Retrieves the current authorization status for biometric authentication. |
| 37 | + /// |
| 38 | + /// - Returns: The current authorization status, indicating whether biometric authentication is authorized, denied, or not determined. |
| 39 | + public override var authorizationStatus: AuthorizationStatus { |
| 40 | + let context = LAContext() |
| 41 | + var error: NSError? |
| 42 | + |
| 43 | + if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) { |
| 44 | + return .authorized |
| 45 | + } else { |
| 46 | + switch error?.code { |
| 47 | + case LAError.Code.biometryLockout.rawValue, LAError.Code.biometryNotAvailable.rawValue, |
| 48 | + LAError.Code.biometryNotEnrolled.rawValue: |
| 49 | + return .denied |
| 50 | + default: |
| 51 | + return .notDetermined |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /// Requests permission for biometric authentication with a completion handler. |
| 57 | + /// |
| 58 | + /// - Parameters: |
| 59 | + /// - completion: A closure to be called once the request is processed. |
| 60 | + /// The closure takes a boolean indicating whether the permission was granted |
| 61 | + /// and an optional error in case of failure. |
| 62 | + public override func requestPermission(completion: @escaping (Bool, Error?) -> Void) { |
| 63 | + let context = LAContext() |
| 64 | + |
| 65 | + let localizedReason = "Authenticate to access biometric features" |
| 66 | + |
| 67 | + context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: localizedReason) { success, error in |
| 68 | + DispatchQueue.main.async { |
| 69 | + if success { |
| 70 | + completion(true, nil) // Authorized (true), no error |
| 71 | + } else { |
| 72 | + completion(false, error) // Not authorized (false), with error |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments