LAContext可以评估策略和Swift 2

时间:2015-06-29 10:07:41

标签: ios swift swift2

这是我在Swift中的代码:

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

使用Swift2我将代码更改为:

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

但是我收到以下错误:

  

调用可以抛出,但没有标记为'try'并且未处理错误

我做错了什么?

3 个答案:

答案 0 :(得分:7)

你需要做这样的事情:

do {
    try laContext.canEvaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics)

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

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

编辑:要获得有关错误的更多信息,请在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
}

(您可以通过CMD点击LAError

查看所有可能的错误 编辑:在XCode 7 beta 5/6中,此方法不再抛出,但是NSErrorPointer作为最后一个参数(NSURL的{​​{1}}也是如此我知道的。)但是,如果您愿意,可以将checkResourceIsReachableAndReturnError扩展为像以前一样投掷方法:

LAContext

答案 1 :(得分:0)

试试这段代码:

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

答案 2 :(得分:0)

对于Swift 3,我这样做:

try-catch