LAContext canEvaluatePolicy 和 Swift 2

LAContext canEvaluatePolicy and Swift 2

这是我在 Swift 中的代码:

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

使用 Swift2 我将代码更改为如下所示:

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

但我收到以下错误:

Call can throw, but it is not marked with 'try' and the error is not handled

我做错了什么?

你需要做这样的事情:

do {
    try laContext.canEvaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics)

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

所有返回 Bool 并以 inout NSError? 作为最后一个参数的方法都会自动转换 (Swift 2) 以抛出错误,因此该参数已被删除。 Bool 也是多余的,因为它等于 inout NSError? 是否为 nil

编辑:要获取有关错误的更多信息,请在捕获中使用它:

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
}

(您可以通过CMD点击LAError

查看所有可能的错误

编辑:在 XCode 7 beta 5/6 中,此方法不再抛出,但将 NSErrorPointer 作为最后一个参数(NSURLcheckResourceIsReachableAndReturnError 出于我不知道的原因)。但是,如果您愿意,您可以扩展 LAContext 以像以前一样制作投掷方法:

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

试试这个代码:

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

对于Swift 3,我这样做:

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:
            ...
            }
        }
    }
}