根据我的经验,您需要将
authentication
相关代码和其他UIViewController
代码分开。我建议singleton
为 Bio- authentication
matric(TouchID 和 FaceID)创建一个基于块的类
请参阅基于块的身份验证库BiometricAuthentication供您参考。
我建议将所有与身份验证相关的代码保留在Login
屏幕中。
请参阅下面的自动登录代码。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if isRemmberMe{
BioMetricAuthenticator.authenticateWithBioMetrics(reason: "") { (result) in
switch result {
case .success( _):
print("Redirect into dashboard screen")
case .failure(let error):
print("Authentication Failed")
}
}
}
}
如果您采用这种方法,则无需在
AppDelegate.swift
文件中编写额外的代码,因为您rootViewController
总是在登录屏幕。只需设置您的初始控制器登录屏幕
storyboard
更新:1
问题:这是一个正确的方法吗?
是的,这是一种正确的方法,但请记住生物矩阵认证的代码集中化。
问题:如果应用程序状态更改,我如何管理 TouchID 或 FaceID 管理
如果应用程序状态已更改,您可以使用applicationWillEnterForeground
OR
。applicationDidBecomeActive
还有一件事,我想在上面提到,当用户重新打开应用程序时,也会调用这两种方法。如果您想完全限制用户访问应用程序内容,请使用,applicationWillEnterForeground()
否则您可以使用applicationDidBecomeActive
更新:2
UIView
如果要限制应用内容,则需要手动添加虚拟模糊。
代码:
let blurEffect = UIBlurEffect(style: .Light)
let blurVisualEffectView = UIVisualEffectView(effect: blurEffect)
blurVisualEffectView.frame = view.bounds
self.view.addSubview(blurVisualEffectView)
认证成功则移除
blurVisualEffectView.removeFromSuperview()