Swift 应用程序在关闭时有奇怪的行为

Vip*_*a74 5 ios touch-id swift face-id

我正在开发一个在开头使用 Face/Touch ID 的应用程序。我通过将此 func 添加到我的 MainViewController() 来实现此目的:

let context = LAContext()

    if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: nil) {
        context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: "Verifying") { (success, err) in
            if success {
                DispatchQueue.main.async {
                    self.loginSuccessfull()
                    self.button.removeFromSuperview()
                }
            } else {
                if let err = err {
                    print(err)
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

这在 ViewDidLoad 和按钮中都会被调用,如本视频所示。

正如你所看到的,当我尝试关闭我的应用程序时,它有一个非常奇怪的行为,我确定这是由 FaceID 引起的。

有什么建议可以解决这个问题吗?

崩溃日志:

Error Domain=com.apple.LocalAuthentication Code=-4 "Caller moved to background." UserInfo={NSLocalizedDescription=Caller moved to background.}
Run Code Online (Sandbox Code Playgroud)

Gal*_*ich 3

我相信我已经通过延迟评估找到了该问题的解决方案。

我注意到,当我在评估之前在 UI 中出现任何类型的延迟时(例如:在显示面容 ID 警报之前向上移动徽标的动画),崩溃会完全停止。

所以我做了另一个延迟测试,如下所示:

override func viewDidAppear(_ animated: Bool) {
    let context = LAContext()
    if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil) {
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
            context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Biometric test") { success, error in
                DispatchQueue.main.async {
                    if success {
                        doSome()
                    } else {
                        if let error = error { print(error) }
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

通过该实施,我的崩溃为零。

*注意:我还尝试了不同的延迟时间,从 0.1 到 2.0 秒,都对我有用。