0

I am trying to add a Face ID/Touch ID/Code to my App, but I'm having some trouble when I terminate it: here's the error I get (with slight variations between one run and another, such as the number of the identifier changing):

Can't end BackgroundTask: no background task exists with identifier 12 (0xc), or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.

I tried following the instructions, but I couldn't figure out what to do. On the internet I found out that this could be an error caused by iOS 13, but I don't have any device running iOS 12 to test it out.

There isn't any crash log in my settings.

Here's the code causing the error:

import UIKit
import LocalAuthentication

class LoginController: VController {

override func viewDidLoad() {
    super.viewDidLoad()

    let context = LAContext()
    var error: NSError?
    let reason = "Identificati"

    if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
        context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { (success, err) in
            DispatchQueue.main.async {
                if success {
                    context.invalidate()
                    let layout = UICollectionViewFlowLayout.init()
                    let dim = (UIScreen.main.bounds.width / 2) - 12
                    layout.itemSize = CGSize(width: dim, height: dim)
                    layout.sectionInset = UIEdgeInsets(top: 6, left: 6, bottom: 6, right: 6)
                    let vc = UINavigationController(rootViewController: ViewController(collectionViewLayout: layout))
                    vc.modalPresentationStyle = .fullScreen
                    self.present(vc, animated: true, completion: nil)
                } else {
                    print(err!.localizedDescription)
                }
            }
        }

    } else {
        if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) {
            context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason) { (success, err) in
                DispatchQueue.main.async {
                    if success {
                        context.invalidate()
                        let layout = UICollectionViewFlowLayout.init()
                        let dim = (UIScreen.main.bounds.width / 2) - 12
                        layout.itemSize = CGSize(width: dim, height: dim)
                        layout.sectionInset = UIEdgeInsets(top: 6, left: 6, bottom: 6, right: 6)
                        let vc = UINavigationController(rootViewController: ViewController(collectionViewLayout: layout))
                        vc.modalPresentationStyle = .fullScreen
                        self.present(vc, animated: true, completion: nil)
                    } else {
                        print(err!.localizedDescription)
                    }
                }
            }

        } else {
            // Nothing worked
        }
    }
  }

}
Vipera74
  • 227
  • 3
  • 17

2 Answers2

1

Call the invalidate() function on your context after you are done with it.

nanibir
  • 234
  • 1
  • 9
0

Related to this thread: iOS : Touch Id is not shown when AppDelegate's open url is invoked

I have similar problem: app crash on terminal when I use authentication. But I haven't gotten any error log at first. Then I remove canEvaluatePolicy(), only keep evaluatePolicy(), then I got error log when app terminal:

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

And by keywords "Caller moved to background" I found a solution. Seems that if evaluation works only when sceneDidBecomeActive(), app would not crash on terminal. Hope this would help.

Maundytime
  • 101
  • 6