// // 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 = .deviceOwnerAuthenticationWithBiometrics 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: ((BiometricAuthenticationError?) -> Void)?) { let context = LAContext() var error: NSError? let msg = "bio_message_title_text".localized() 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 { DispatchQueue.main.async { let bioError = self.setBiometricError(error: err) completion?(bioError) self.delegate?.viewController(vc, didFailWithError: bioError, errorMessage: bioError.message) } } }) } else { KeyChain.shared.save(data: "0", key: .biometricAuth) DispatchQueue.main.async { let bioError = self.setBiometricError(error: error) completion?(bioError) self.delegate?.viewController( vc, didFailWithError: bioError, errorMessage: bioError.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) } } extension BiometricAuthenticationWireframe { private func setBiometricError(error: Error?) -> BiometricAuthenticationError { let bioError: BiometricAuthenticationError if #available(iOS 11.0, *) { switch error { 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 error { 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 } } return bioError } }