iosswifttouch-idface-id

Swift application having weird behavior when closing it


I am developing an application that uses the Face/Touch ID at the opening. I achieved this by adding this func to my 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)
                }
            }
        }
    }

This gets called both in the ViewDidLoad and by a button, as shown in this video.

As you can see tho, when I try to close my App it has a very weird behavior, and I am sure that it is caused by the FaceID.

Any suggestion to fix this?

Crash log:

Error Domain=com.apple.LocalAuthentication Code=-4 "Caller moved to background." UserInfo={NSLocalizedDescription=Caller moved to background.}

Solution

  • I believe I have found a solution for the issue, by delaying the evaluation.

    I noticed that when I have any kind of delay in UI before evaluation (for example: animation that move the logo up before showing the face ID alert) the crash stops altogether.

    So I did another test with delay like so:

    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) }
                        }
                    }
                }
            }
        }
    }
    

    With that implementation I had zero crashes.

    *Note: I also tried different delay times, from 0.1 to 2.0 seconds, all worked for me.