LocalAuthentication like Face ID crashes using swift 6 iOS 18.1

Hello, I have been implementing faceID authentication using LocalAuthentication, and I've noticed that if i use swift 5 this code compiles but when i change to swift 6 it gives me a crash saying this compile error:

i have just created this project for this error purpose so this is my codebase:

import LocalAuthentication
import SwiftUI

struct ContentView: View {
    @State private var isSuccess: Bool = false
    var body: some View {
        VStack {
            if isSuccess {
                Text("Succed")
            } else {
                Text("not succeed")
            }
        }
        .onAppear(perform: authenticate)
    }
    
    func authenticate() {
        let context = LAContext()
        var error: NSError?
        
        if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
            let reason = "We need to your face to open the app"
            context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { sucexd, error in
                
                if sucexd {
                    let success = sucexd
                    Task { @MainActor [success] in
                        isSuccess = success
                    }
                } else {
                    print(error?.localizedDescription as Any)
                }
                
            }
        } else {
            print(error as Any)
        }
    }
}

#Preview {
    ContentView()
}

also i have tried to not use the task block and also gives me the same error. i think could be something about the LAContext NSObject that is not yet adapted for swift 6 concurrency?

also i tried to set to minimal but is the same error

Im using xcode 16.1 (16B40) with M1 using MacOS Seqouia 15.0.1

Help.

Answered by DTS Engineer in 817743022

I think you’re hitting the issue I explain in this post.

If this does resolve your issue, I’d appreciate you filing a bug against Local Authentication framework asking that they annotate this callback correctly. Please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

I think you’re hitting the issue I explain in this post.

If this does resolve your issue, I’d appreciate you filing a bug against Local Authentication framework asking that they annotate this callback correctly. Please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

@DTS Engineer

Hello Quinn thanks for answering, yes changing the method using async works.

i will leave the method that works for me

func authenticate() async {
        let context = LAContext()
        var error: NSError?
        
        if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
            let reason = "We need to verify your identity"
            do {
                let success = try await context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason)
                await MainActor.run {
                    self.isAuthenticated = success
                }
            } catch {
                print(error.localizedDescription)
            }
        } else {
            print("Biometrics not available: \(error?.localizedDescription ?? "Unknown error")")
        }
    }

also i filled the bug before your answering, this is the bug number for the record.

Dec 11, 2024 at 9:48 AM – FB16082108

LocalAuthentication like Face ID crashes using swift 6 iOS 18.1
 
 
Q