如何重构 Face/Touch ID 实现? UIKit iOS 13.2+

问题描述

当付费用户可以启用此“额外”安全级别时,我已对应用实施了触控/面部 ID 身份验证。这是一个简单的 PDF 编辑器应用程序,因此在您成为付费用户并想使用它之前,它没有安全功能

可以通过 UISwitch 在“设置”中启用生物识别身份验证。

enter image description here

这样,我怎么实现就够了?有什么改进的想法吗?

  1. 用户启用后,UISwitchisON 布尔值保存到 UserDefaults
  2. 我已经设置了 AuthenticationManager

`

class AuthenticationManager {
  static let shared = AuthenticationManager()
  var needsAuthentication = UserDefaults.standard.value(forKey: "toggledOn") as? Bool ?? false
  var context = LAContext()

  init(){}
    
  func unlockThe(UIViewController: UIViewController) {
    var error: NSError?
    
    // Getting new context every time I go into the background.
    context = LAContext()
    if context.canEvaluatePolicy(.deviceOwnerAuthentication,error: &error) {
        
        if context.canEvaluatePolicy(.deviceOwnerAuthentication,error: &error) {

            let reason = "Unlock the Printer app".localize
            context.evaluatePolicy(.deviceOwnerAuthentication,localizedReason: reason ) { success,error in

                if success {

                    // Move to the main thread because a state update triggers UI changes.
                    dispatchQueue.main.async { [uNowned self] in
                        UIViewController.dismiss(animated: true,completion: nil)
                    }

                } else {
                    print(error?.localizedDescription ?? "Failed to authenticate")

                    // Fall back to a asking for username and password.
                    // ...
                }
            }
        } else {
            print(error?.localizedDescription ?? "Can't evaluate policy")

            // Fall back to a asking for username and password.
            // ...
        }
    }
    
}
  1. 如您所见,AuthenticationManager 具有反映 UserDefaults 状态的属性,并且基于此值,我会在 2 个位置触发生物识别过程验证。

A) 在 AppDelegate 的存根 applicationWillEnterForeground 中,您可以在这里观察,因此每次我从后台打开应用程序时,它都会被锁定。

   func applicationWillEnterForeground(_ application: UIApplication) {
    if AuthenticationManager.shared.needsAuthentication {
        
        let topController = UIApplication.shared.delegate?.window??.rootViewController?.topViewController()
        
        let lockedScreen = LockedVC()
        lockedScreen.modalPresentationStyle = .overCurrentContext
        
        topController?.present(lockedScreen,animated: false,completion: nil)
        
    }

B) 从我的应用程序“主页”的 viewDidLoad 开始,基本上与我在 AppDelegate 中的条件逻辑相同(为了该用户将例如强制使用该应用程序)

LockedVC 只是带有一些应用已锁定信息的模糊视图。当生物特征验证成功时,它只会自行关闭用户会在他离开的页面上。

是否有任何改进此流程的想法或有关后备的任何建议?应用可从 iOS 13.2 获得,所以我想没有配备此 iOS 且没有生物识别功能的设备。

我还没有涉及的是,如果用户没有设置生物特征。然后我想我可以在他想要启用此功能通知用户他需要设置它的事实,然后允许他更改 UISwitch 的值。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)