TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

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      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.