LAContext canEvaluatePolicy and Swift 2

This is my code in Swift:

if (LAContext().canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics)) {
  return true;
}

With Swift2, I changed my code to look like this:

if #available(iOS 8, *) {
            if (LAContext().canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics)) {
                return true;
            }
        }

But I get the following error:

A call may cause, but it is not marked with a "try", and the error is not processed.

What am I doing wrong?

+4
source share
3 answers

You need to do something like this:

do {
    try laContext.canEvaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics)

    // Call evaluatePolicy here
} catch {
    print("Cannot evaluate policy, error: \(error)")
}

All methods that returned Booland have inout NSError?as the last parameter were automatically converted (Swift 2) to throw an error, so the parameter was deleted. It Boolwas also redundant as it was equal to inout NSError?nil

EDIT: for more error information, use this in catch:

switch LAError(rawValue: error.code)! {
case .AuthenticationFailed:
    break
case .UserCancel:
    break
case .UserFallback:
    break
case .SystemCancel:
    break
case .PasscodeNotSet:
    break
case .TouchIDNotEnrolled:
    break
default:
    break
}

(You can see all possible errors by clicking CMD on LAError

EDIT: XCode 7 beta 5/6 , NSErrorPointer ( NSURL checkResourceIsReachableAndReturnError ). LAContext, , , :

extension LAContext {
    func canEvaluatePolicyThrowing(policy: LAPolicy) throws {
        var error : NSError?
        canEvaluatePolicy(policy, error: &error)
        if let error = error { throw error }
    }
}
+7

:

do {

        try touchIDContext.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics)


    //Comprobar la repuesta de esa autentificacion

    touchIDContext.evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: mensaje, reply: { (success, ErrorType) -> Void in


            if success
            {
               // Autentificacion correcta
                alert.title = "Genial!"
                alert.message = "Te has autentificado correctamente"

                // Mostramos este Alerview

                self.presentViewController(alert, animated: true, completion: nil)

            }

            else
            {
                //AUTENTIFICACION FALLIDA
                //PASAMOS VALORES AL ALERTVIEW
                alert.title = "AUTENTIFICACION FALLIDA!!"

                //OFRECEMOS MAS INFORMACION SOBRE EL FALLO DE AUTENTIFICAICON

                switch ErrorType!.code {

                case LAError.UserCancel.rawValue: alert.message = "Usuario Cancela"
                case LAError.AuthenticationFailed.rawValue: alert.message = "Autentificacion Fallida!"
                case LAError.PasscodeNotSet.rawValue: alert.message = "Password no configurado"
                case LAError.SystemCancel.rawValue: alert.message = "Error de sistema"
                case LAError.UserFallback.rawValue:alert.message = "Usuario selecciona contrasen"
                default:alert.message = "Imposible Autentificarse"

            }

                //Mostramos el AlertView

                self.presentViewController(alert, animated: true, completion: nil)

            }
        }) // cierre del clousure





    } // cierre del do

    catch {

        print("Cannot evaluate policy, error: \(error)")
    } // cierre del catch
0

For Swift 3, I do this:

context.evaluatePolicy(policy, localizedReason: ...) { (success: Bool, error: Error?) in
    DispatchQueue.main.async {
        if success {
            ...
        } else if let error = error as? LAError {
            switch error.code {
            case LAError.authenticationFailed:
            ...
            }
        }
    }
}
0
source

All Articles