重试 FaceID 后,我立即看到解锁的应用程序内容

After Retrying the FaceID I see unlocked apps content in a moment

我再次尝试面容 ID 时看到 "Unlocked" 文字。如何显示空白屏幕或启动画面而不是它?

我的代码如下:

import Foundation
import LocalAuthentication

class UserAuthentification{

    func authenticate(){

        let context = LAContext()
        var error: NSError?

        guard context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) else {
            print("No Biometric Sensor Has Been Detected. This device does not support FaceID/TouchID.")
            return
        }

        context.evaluatePolicy(LAPolicy.deviceOwnerAuthentication, localizedReason: "Only device owner is allowed", reply: { (success, error) -> Void in

            if( success ) {
                print("FaceID/TouchID. You are a device owner!")
            } else {

                // Check if there is an error
                if let errorObj = error {
                    print("Error took place. \(errorObj.localizedDescription)")
                }
                if LAError.Code.userCancel.rawValue == -2{
                self.authenticate()
            }

            }
        })
    }
}

struct ContentView: View {


    var variable : UserAuthentification

    var body: some View {

// 重试 FaceID 后看到的这段文字

                Text("Unlocked")
        .onAppear(perform: variable.authenticate)

    }

}

感谢您提前回答!)

我会修改您的 UserAuthentication class 以便它公开一个 @Observable 对象来指示已验证状态。

您在检查用户取消结果时也有错误 - 您只是在检查常量自身的值。您需要检查 LAError

中的 code
import Foundation
import LocalAuthentication

class UserAuthenticator: ObservableObject {

    @Published private(set) var isAuthenticated = false
    @Published private(set) var error: Error?

    func authenticate(){

        let context = LAContext()
        var error: NSError?

        guard context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) else {
            print("No Biometric Sensor Has Been Detected. This device does not support FaceID/TouchID.")
            return
        }

        context.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Only device owner is allowed", reply: { (success, error) -> Void in

            DispatchQueue.main.async {

                self.error = error

                if( success ) {
                    print("FaceID/TouchID. You are a device owner!")
                    self.isAuthenticated = true
                } else {

                    // Check if there is an error
                    if let errorObj = error as? LAError {
                        print("Error took place. \(errorObj.localizedDescription)")
                        if errorObj.code == .userCancel {
                            self.authenticate()
                        }
                    }
                }
            }
        })
    }

    func deauthenticate() {
        self.isAuthenticated = false
    }
}

然后您可以将此 class 的一个实例作为模型对象注入到您的视图中,并使用 isAuthenticated 属性 来控制视图状态:

import SwiftUI

struct ContentView: View {

    @ObservedObject var authenticator: UserAuthenticator

    var body: some View {
        HStack {
            if authenticator.isAuthenticated {
                VStack {
                    Text("Welcome")
                    Button(action: {self.authenticator.deauthenticate()}) {
                        Text("Logout")
                    }
                }
            } else {
                VStack {
                    Text("Please authenticate").onAppear {
                        self.authenticator.authenticate()
                    }
                    if self.authenticator.error != nil {
                        Text(self.authenticator.error!.localizedDescription)
                    }
                }
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView(authenticator: UserAuthenticator())
    }
}

您需要从场景委托中注入 UserAuthenticator 实例,就像我在预览提供程序代码中所做的那样。

这是一个简单的例子;如果生物识别身份验证不可用,您需要考虑重试限制和回退,但这应该可以帮助您入门。