// // BiometricAuthenticationWireframe.swift // GME Remit // // Created by InKwon Kim on 30/03/2019. //Copyright © 2019 Gobal Money Express Co. Ltd. All rights reserved. // import UIKit import LocalAuthentication class BiometricAuthenticationWireframe { weak var view: UIViewController! private var policy: LAPolicy = .deviceOwnerAuthentication private weak var delegate: BiometricAuthenticationViewControllerDelegate? } extension BiometricAuthenticationWireframe: BiometricAuthenticationWireframeInput { var storyboardName: String {return "BiometricAuthentication"} func getMainView() -> UIViewController { let service = BiometricAuthenticationService() let interactor = BiometricAuthenticationInteractor(service: service) let presenter = BiometricAuthenticationPresenter() let viewController = viewControllerFromStoryboard(of: BiometricAuthenticationViewController.self) viewController.presenter = presenter interactor.output = presenter presenter.interactor = interactor presenter.wireframe = self presenter.view = viewController self.view = viewController return viewController } func showBiometricAuthentication(completion: ((Error?) -> Void)?) { let context = LAContext() var error: NSError? let msg = "Confirm your fingerprint or face to authenticate." let vc = self.view as? BiometricAuthenticationViewController ?? BiometricAuthenticationViewController() if let bioPolicy = delegate?.doSelectLocalAuthenticationPolicy(vc){ switch bioPolicy { case .deviceOwnerAuthentication: self.policy = .deviceOwnerAuthentication case .deviceOwnerAuthenticationWithBiometrics: self.policy = .deviceOwnerAuthenticationWithBiometrics } } if context.canEvaluatePolicy(self.policy, error: &error) { context.evaluatePolicy(self.policy, localizedReason: msg, reply: { (success, err) in if success { DispatchQueue.main.async { completion?(nil) self.delegate?.didComplete(vc) } } else { let bioError: BiometricAuthenticationError if #available(iOS 11.0, *) { switch err { case LAError.authenticationFailed?: bioError = .authenticationFailed case LAError.userCancel?: bioError = .userCancel case LAError.userFallback?: bioError = .userFallback case LAError.biometryNotAvailable?: bioError = .biometryNotAvailable case LAError.biometryNotEnrolled?: bioError = .biometryNotEnrolled case LAError.biometryLockout?: bioError = .biometryLockout default: bioError = .notBeConfigured } } else { switch err { case LAError.authenticationFailed?: bioError = .authenticationFailed case LAError.userCancel?: bioError = .userCancel case LAError.userFallback?: bioError = .userFallback case LAError.touchIDNotAvailable?: bioError = .biometryNotAvailable case LAError.touchIDNotEnrolled?: bioError = .biometryNotEnrolled case LAError.touchIDLockout?: bioError = .biometryLockout default: bioError = .notBeConfigured } } completion?(bioError) self.delegate?.viewController(vc, didFailWithError: bioError, errorMessage: bioError.message) } }) } else { self.delegate?.viewController(vc, didFailWithError: error!, errorMessage: error?.localizedDescription) } } func openWithDelegate( on window: UIWindow, delegate: BiometricAuthenticationViewControllerDelegate ){ let vc = getMainView() self.delegate = delegate (vc as? BiometricAuthenticationViewController)?.delegate = delegate window.rootViewController = vc } func openWithDelegate( on viewConroller: UIViewController, delegate: BiometricAuthenticationViewControllerDelegate ){ let vc = getMainView() self.delegate = delegate (vc as? BiometricAuthenticationViewController)?.delegate = delegate viewConroller.present(vc, animated: true, completion: nil) } }