从后台返回时使用 FaceID 或 TouchID 对登录用户进行身份验证
Using FaceID or TouchID to authenticate a signed in user when coming back from background
我有一个需要非常安全的应用程序。因此,将应用置于前台的用户需要通过 TouchID、FaceID 或他们的 phone 个人识别码验证自己才能继续。
我能够显示和删除 coverVC
,但在我的 BackgroundViewController
(充当 coverVC
的那个)中,我想提示用户生物当应用程序在前台时进行身份验证
我正在尝试将 handler
函数传递给 BackgroundViewController
以在正确的时间触发,但它不起作用,我不清楚我做错了什么。会喜欢这里的任何帮助。谢谢!
在我的 AppDelegate
:
func applicationWillResignActive(_ application: UIApplication) {
coverVC = BackgroundViewController()
coverWindow = UIWindow(frame: UIScreen.main.bounds)
let existingTopWindow = UIApplication.shared.windows.last
coverWindow?.windowLevel = existingTopWindow!.windowLevel + 1
coverVC!.view.frame = coverWindow!.bounds
coverWindow?.rootViewController = coverVC
coverWindow?.makeKeyAndVisible()
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
if coverWindow != nil {
self.coverVC?.handler = {
DispatchQueue.main.async {
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseInOut, animations: {
self.coverVC?.view.alpha = 0
}) { _ in
self.coverWindow!.isHidden = true
self.coverWindow!.rootViewController = nil
self.coverWindow = nil
self.coverVC = nil
}
}
}
self.coverVC?.authenticateReturningUser()
}
}
在我的 BackgroundViewController.swift
:
class BackgroundViewController: UIViewController {
var handler: (() -> Void) = {}
(viewDidLoad and other stuff in here)
func authenticateReturningUser() {
let context = LAContext()
var error: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) {
let reason = "Verify that this is your device to continue."
context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason) { [weak self] success, error in
DispatchQueue.main.async {
if success {
if let _self = self {
_self.handler()
} else {
print("this is hitting")
}
}
}
}
}
private func showError(title: String, message: String, buttonText text: String = "Okay") {
let ac = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)
ac.addAction(UIAlertAction(title: text, style: .default) { _ in
self.handler()
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "LogOut"), object: nil)
})
present(ac, animated: true)
}
编辑:
如果我将 [weak self]
设置为 [unowned self]
并调用 self.handler()
,我得到:
Fatal error: Attempted to read an unowned reference but the object was already deallocated
您的 context
对象正在被释放。
将您的代码更改为此,以便上下文对象具有引用
var context: LAContext!
func authenticateReturningUser() {
context = LAContext()
var error: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) {
let reason = "Verify that this is your device to continue."
context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason) { [weak self] success, error in
DispatchQueue.main.async {
if success {
if let _self = self {
_self.handler()
} else {
print("this is hitting")
}
}
}
}
}
}
我有一个需要非常安全的应用程序。因此,将应用置于前台的用户需要通过 TouchID、FaceID 或他们的 phone 个人识别码验证自己才能继续。
我能够显示和删除 coverVC
,但在我的 BackgroundViewController
(充当 coverVC
的那个)中,我想提示用户生物当应用程序在前台时进行身份验证
我正在尝试将 handler
函数传递给 BackgroundViewController
以在正确的时间触发,但它不起作用,我不清楚我做错了什么。会喜欢这里的任何帮助。谢谢!
在我的 AppDelegate
:
func applicationWillResignActive(_ application: UIApplication) {
coverVC = BackgroundViewController()
coverWindow = UIWindow(frame: UIScreen.main.bounds)
let existingTopWindow = UIApplication.shared.windows.last
coverWindow?.windowLevel = existingTopWindow!.windowLevel + 1
coverVC!.view.frame = coverWindow!.bounds
coverWindow?.rootViewController = coverVC
coverWindow?.makeKeyAndVisible()
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
if coverWindow != nil {
self.coverVC?.handler = {
DispatchQueue.main.async {
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseInOut, animations: {
self.coverVC?.view.alpha = 0
}) { _ in
self.coverWindow!.isHidden = true
self.coverWindow!.rootViewController = nil
self.coverWindow = nil
self.coverVC = nil
}
}
}
self.coverVC?.authenticateReturningUser()
}
}
在我的 BackgroundViewController.swift
:
class BackgroundViewController: UIViewController {
var handler: (() -> Void) = {}
(viewDidLoad and other stuff in here)
func authenticateReturningUser() {
let context = LAContext()
var error: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) {
let reason = "Verify that this is your device to continue."
context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason) { [weak self] success, error in
DispatchQueue.main.async {
if success {
if let _self = self {
_self.handler()
} else {
print("this is hitting")
}
}
}
}
}
private func showError(title: String, message: String, buttonText text: String = "Okay") {
let ac = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)
ac.addAction(UIAlertAction(title: text, style: .default) { _ in
self.handler()
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "LogOut"), object: nil)
})
present(ac, animated: true)
}
编辑:
如果我将 [weak self]
设置为 [unowned self]
并调用 self.handler()
,我得到:
Fatal error: Attempted to read an unowned reference but the object was already deallocated
您的 context
对象正在被释放。
将您的代码更改为此,以便上下文对象具有引用
var context: LAContext!
func authenticateReturningUser() {
context = LAContext()
var error: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) {
let reason = "Verify that this is your device to continue."
context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason) { [weak self] success, error in
DispatchQueue.main.async {
if success {
if let _self = self {
_self.handler()
} else {
print("this is hitting")
}
}
}
}
}
}