问题描述
当应用程序进入后台时,我试图锁定我的应用程序;当应用程序位于前台时,用户可以使用生物特征识别(FaceID / TouchID)进行身份验证。
import LocalAuthentication
import SwiftUI
struct ContentView: View {
@State var isUnlocked = false
var body: some View {
if isUnlocked {
// if the app is unlocked,app shows the text "Unlocked"
Text("Unlocked")
.font(.title)
// app gets locked when the app goes to background
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willResignActiveNotification)) { _ in
self.isUnlocked = false
print("locked the app")
}
} else {
// if the app is locked,app shows the text "Locked" and app should show prompt FaceID/TouchID prompt
Text("LOCKED")
.font(.title)
// FaceID prompt shows when the app opens (from Non-running state)
.onAppear(perform: authenticate)
// FaceID prompt shows when the app coming from background to foreground
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
authenticate()
print("back inside the app")
}
}
}
// I use this to prompt the biometric authentication
func authenticate() {
let context = LAContext()
var error: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics,error: &error) {
let reason = "We need to unlock your data"
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics,localizedReason: reason) { success,authenticationError in
dispatchQueue.main.async {
if success {
// app gets unlocked if biometrix authentication succeeded
self.isUnlocked = true
} else {
// authentication Failed
}
}
}
} else {
// no biometrics
}
}
}
当用户打开应用程序(处于非运行状态)时,FaceID提示会立即显示而不会出现任何延迟。 但是,当应用程序从后台运行到前台时,FaceID提示大约需要5秒钟才能显示。我使用相同的功能authenticate()
来提示生物特征认证,并且Xcode控制台会打印{{1} },当应用程序从后台运行到前台时。
那么,什么原因导致FaceID延迟?
请帮助我。谢谢。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)