7

I had written the following code snippet

if ([contextNew canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error])
{
    [contextNew evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:NSLocalizedString(@"",nil) reply:^(BOOL success, NSError *error)
    {
        if(success)
        {

        }
        else
        {

        }
    }
}

it comes in sucess block after verification of face.I want to handle face id permission alert. I want to get the Yes or permission granted method for face id . As we get something like AVAuthorizationStatus for camera

This is the permission popup

Krunal
  • 77,632
  • 48
  • 245
  • 261
Pratik
  • 103
  • 1
  • 9

1 Answers1

1

Not sure, will this solve your problem or not, but I've following options to detect LAAuthentication Error type (in Swift, you need to convert it into Objective-C)

laContext.evaluatePolicy(biometricsPolicy, localizedReason: localizedReason, reply: { (isSuccess, error) in
    if let laError = error {
       // handle error
       self.showLAError(laError: laError)
    } else if isSuccess {
      // handle success
    }
})


// function to detect an error type
func showLAError(laError: Error) -> Void {
    
    var message = ""
    
    switch laError {
        
    case LAError.appCancel:
        message = "Authentication was cancelled by application"
        
    case LAError.authenticationFailed:
        message = "The user failed to provide valid credentials"
        
    case LAError.invalidContext:
        message = "The context is invalid"
        
    case LAError.passcodeNotSet:
        message = "Passcode is not set on the device"
        
    case LAError.systemCancel:
        message = "Authentication was cancelled by the system"
        
    case LAError.touchIDLockout:
        message = "Too many failed attempts."
        
    case LAError.touchIDNotAvailable:
        message = "TouchID is not available on the device"
        
    case LAError.userCancel:
        message = "The user did cancel"
        
    case LAError.userFallback:
        message = "The user chose to use the fallback"
        
    default:
        
        if #available(iOS 11.0, *) {
            
            switch laError {
                
            case LAError.biometryNotAvailable:
                message = "Biometry is not available"
                
            case LAError.biometryNotEnrolled:
                message = "Authentication could not start, because biometry has no enrolled identities"
                
            case LAError.biometryLockout:
                message = "Biometry is locked. Use passcode."

            default:
                message = "Did not find error code on LAError object"
            }
            
        }
        message = "Did not find error code on LAError object"
        
        
    }
    
    //return message
    print("LAError message - \(message)")
    
}

Here is list of all types of LAError for Objective-C

Edit:

If you are trying with Simulator, then see here how to test Face-Id:

Here is working Objective-C code. Try this and see:

LAContext *laContext = [[LAContext alloc] init];

NSError *error;

if ([laContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
    
    if (error != NULL) {
        // handle error
        //[self showError:error];
    } else {
        
        if (@available(iOS 11.0.1, *)) {
            if (laContext.biometryType == LABiometryTypeFaceID) {
                //localizedReason = "Unlock using Face ID"
                NSLog(@"FaceId support");
            } else if (laContext.biometryType == LABiometryTypeTouchID) {
                //localizedReason = "Unlock using Touch ID"
                NSLog(@"TouchId support");
            } else {
                //localizedReason = "Unlock using Application Passcode"
                NSLog(@"No Biometric support");
            }
        } else {
            // Fallback on earlier versions
        }
        
        
        [laContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"Test Reason" reply:^(BOOL success, NSError * _Nullable error) {
            
            if (error != NULL) {
                // handle error
            } else if (success) {
                // handle success response
            } else {
                // handle false response
            }
        }];
    }
}
Community
  • 1
  • 1
Krunal
  • 77,632
  • 48
  • 245
  • 261
  • I have added the code but I want to get the Yes button clicked action for Face ID permission pop up which I have attached to the question.When we click on don't allow it goes in case LAError.touchIDNotAvailable: but when we tap Yes it doesn't goes any were it goes in success block when user successfully authenticate the face or Touch ID. I want the Yes button clicked action for Face ID permission pop up.Please see the attached permission pop up image. – Pratik Feb 20 '18 at 06:50
  • @Pratik - See edit, if you are trying with simulator. – Krunal Feb 20 '18 at 09:29
  • We are trying on device not simulator.It comes in success blog after proper face authentication. – Pratik Feb 20 '18 at 11:03
  • @Pratik What is exact problem now? Is it working fine or still (with my code) showing you same popup message? – Krunal Feb 20 '18 at 11:07