Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Face ID evaluation process not working properly

I'm trying to get if Face ID or Touch ID succeeded in the function below

func authenticate() -> Bool{

    let context = LAContext()
    var error: NSError?

    guard context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) else {
        return false
    }
    var returnValue = false
    let reason = "Face ID authentication"
    context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) 
    {
        isAuthorized, error in
        guard isAuthorized == true else {
            return print(error)
        }
        returnValue = true
        print("success")
    }
    return returnValue        
}

but even when it succeeds with this code it skips the returnValue = true is passed later which results a false return. why does this happen? and how can I fix this code to make it work like it is suppose to?

the code above is from this link just in case this person was watching, thank you.

like image 724
Yuto Avatar asked Aug 30 '18 08:08

Yuto


People also ask

What do I do if my Face ID is not working?

What To Do When Face ID Is Not Working On iPhone: The Fix! Restart Your iPhone. Make Sure You’re Holding Your iPhone Far Enough Away From Your Face. Make Sure There Are No Other Faces Around You. Remove Any Clothing Or Jewelry Covering Your Face. Check Lighting Conditions.

Why is Face ID not working on my iPhone?

When Face ID is not working on your iPhone, it means that you cannot unlock the device with your face. If you encounter the same issue, the solutions we shared above can help you to solve it quickly. Apeaksoft iOS System Recovery is the powerful way to repair your iPhone without data loss.

How do I Check my Face ID settings?

To check your Face ID settings, go to Settings > Face ID & Passcode. Make sure that Face ID is set up and that the features you’re trying to use Face ID with are turned on.

Why doesn't Face ID work with sunglasses?

Face ID doesn't work if anything is covering your mouth and nose, like a face mask. If you're wearing a face mask, you'll be asked to enter your passcode automatically after swiping up. Face ID works with many sunglasses.


1 Answers

Working code of Touch ID & Face ID LocalAuthentication

(swift 4.0 & 5.0+ Code)

Note : Privacy - Face ID Usage Description key add in Info.plist

Use

self.Authenticate { (success) in
     print(success)
}

Local Authentication Function

import LocalAuthentication

func Authenticate(completion: @escaping ((Bool) -> ())){
    
    //Create a context
    let authenticationContext = LAContext()
    var error:NSError?
    
    //Check if device have Biometric sensor
    let isValidSensor : Bool = authenticationContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error)
    
    if isValidSensor {
        //Device have BiometricSensor
        //It Supports TouchID
        
        authenticationContext.evaluatePolicy(
            .deviceOwnerAuthenticationWithBiometrics,
            localizedReason: "Touch / Face ID authentication",
            reply: { [unowned self] (success, error) -> Void in
                
                if(success) {
                    // Touch / Face ID recognized success here
                    completion(true)
                } else {
                    //If not recognized then
                    if let error = error {
                        let strMessage = self.errorMessage(errorCode: error._code)
                        if strMessage != ""{
                            self.showAlertWithTitle(title: "Error", message: strMessage)
                        }
                    }
                    completion(false)
                }
        })
    } else {
        
        let strMessage = self.errorMessage(errorCode: (error?._code)!)
        if strMessage != ""{
            self.showAlertWithTitle(title: "Error", message: strMessage)
        }
    }
    
}

Handle Error Codes with Messages

//MARK: TouchID error
func errorMessage(errorCode:Int) -> String{
    
    var strMessage = ""
    
    switch errorCode {
        
    case LAError.Code.authenticationFailed.rawValue:
        strMessage = "Authentication Failed"
        
    case LAError.Code.userCancel.rawValue:
        strMessage = "User Cancel"
        
    case LAError.Code.systemCancel.rawValue:
        strMessage = "System Cancel"
        
    case LAError.Code.passcodeNotSet.rawValue:
        strMessage = "Please goto the Settings & Turn On Passcode"
        
    case LAError.Code.biometryNotAvailable.rawValue:
        strMessage = "TouchI or FaceID DNot Available"
        
    case LAError.Code.biometryNotEnrolled.rawValue:
        strMessage = "TouchID or FaceID Not Enrolled"
        
    case LAError.Code.biometryLockout.rawValue:
        strMessage = "TouchID or FaceID Lockout Please goto the Settings & Turn On Passcode"
        
    case LAError.Code.appCancel.rawValue:
        strMessage = "App Cancel"
        
    case LAError.Code.invalidContext.rawValue:
        strMessage = "Invalid Context"
        
    default:
        strMessage = ""
        
    }
    return strMessage
}

Show alert message

//MARK: Show Alert
func showAlertWithTitle( title:String, message:String ) {
    let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
    
    let actionOk = UIAlertAction(title: "OK", style: .default, handler: nil)
    alert.addAction(actionOk)
    self.present(alert, animated: true, completion: nil)
}
like image 165
Nikunj Kumbhani Avatar answered Nov 11 '22 03:11

Nikunj Kumbhani