UnlockManager.swift 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import Combine
  2. import LocalAuthentication
  3. protocol UnlockManager {
  4. func unlock() -> AnyPublisher<Void, Error>
  5. }
  6. struct UnlockError: Error {
  7. let error: Error?
  8. }
  9. final class BaseUnlockManager: UnlockManager {
  10. func unlock() -> AnyPublisher<Void, Error> {
  11. Future { promise in
  12. let context = LAContext()
  13. var error: NSError?
  14. let handler: (Bool, Error?) -> Void = { success, error in
  15. if success {
  16. promise(.success(()))
  17. } else {
  18. promise(.failure(UnlockError(error: error)))
  19. }
  20. }
  21. let reason = "We need to make sure you are the owner of the device."
  22. if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) {
  23. context.evaluatePolicy(
  24. .deviceOwnerAuthentication,
  25. localizedReason: reason,
  26. reply: handler
  27. )
  28. } else {
  29. handler(true, nil)
  30. }
  31. }
  32. .eraseToAnyPublisher()
  33. }
  34. }