UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Simulator issue: context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) keeps returning false

Forums > 100 Days of SwiftUI

I'm currently doing the FaceID authentication portion of the BucketList project and I can't seem to make the FaceID permission prompt to appear in the Simulator. When I checked context.canEvaluatePolicy(...), it kept returning false and as a result, context.evaluatePolicy(...) was not called.

  • I've already added the Privacy - FaceID Usage Description row in the Info section of the project target
  • The Enrolled option in the Features > FaceID is already toggled on
  • I've also rebuild and rerun app as well

I think I might be missing something because weirdly enough, sometimes (very rarely) the prompt does appear but when I run the app again, it's not there anymore.

Here's the code:

import SwiftUI
import MapKit
import LocalAuthentication

struct ContentView: View {
    @State private var isUnlocked = false

    func authenticate() {

        let context = LAContext()

        var error: NSError?

        print(context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error))

        if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
            let reason = "We need to unlock your data."

            context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics , localizedReason: reason) { success, authenticationError in

                if success {

                } else {

                }

            }

        } else {
            print(context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error))

            }}

    var body: some View {
        VStack {
            if isUnlocked {
                Text("Unlocked")
            } else {
                Text("Locked")
            }
        }.onAppear(perform: authenticate)

    }
}

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

1      

Okay, I somehow got it to work.

Before, I was simulating iPhone 13 Pro; but when I tried iPhone 13 and iPhone 13 Pro Max instead, both worked fine and the prompt appeared.

Weird but moving on.

1      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.