Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LAContext evaluatePolicy does not always prompt user

Tags:

ios

touch-id

In my iOS 7 iPad app LAContext:evaluatePolicy sometimes returns SUCCESS without prompting the user to touch the ID button. And the Apple docs say “Evaluating a policy MAY involve prompting the user…”.

My authentication policy is set to LAPolicyDeviceOwnerAuthenticationWithBiometrics, the only choice I see. Why wouldn’t this prompt the user to touch the ID button every time I call evaluatePolicy? Is there a way I can require this behavior?

like image 826
David U Avatar asked Jan 29 '16 21:01

David U


2 Answers

I have experienced a similar problem. It is possible that you are declaring a global variable something like

let authenticationContext = LAContext()

and then use authenticationContext within your class methods and functions.

I have started declaring the constant in each function I use it like

func someAuthFunc() {
let authenticationContext = LAContext()
...

and my problem was solved. I was asked each time I requested evaluateForContext ...

I hope this helps.

Cheers

like image 145
Sasho Avatar answered Nov 15 '22 14:11

Sasho


For who have the same issue

It just happens from iOS 13 and above. The solution is trying to call evaluate function twice like this:

let systemVersion = UIDevice.current.systemVersion
// Trick here: Try to do a pre-evaluate
if systemVersion.compare("13.0", options: .numeric) != .orderedAscending {
    context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: "Authenticate to open the app", reply: { (_, _) in
         //Ignore callback here
     })
}

context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: "Authenticate to open the app", reply: { (success, error) in
    // Handle callback here
})

Tested and work well for all iOS 13.x.x versions so far.

like image 31
jacob Avatar answered Nov 15 '22 13:11

jacob