每当faceid / touchid身份验证失败时如何获取回调

时间:2019-09-30 08:32:08

标签: ios swift iphone touch-id face-id

我们有一个要求,如果Face ID或Touch ID连续5次失败,我们必须封锁用户24小时。

在Face ID中,我使用后备功能再次调用身份验证管理器,并且在总共6次尝试(每次调用两次)之后,我使用后备方法本身阻止了用户。

在Touch ID中,第三次失败时,我回电给Authentication failed。我再次致电身份验证管理器,在第5次尝试中,我收到了回拨到Lockout的电话,在该电话中可以阻止用户。

在Face ID和Touch ID中,是否有任何共同的方式可以使我在每次出现单个故障后都得到回调,以便在第5次故障本身上阻止用户?

//MARK:- Check if user has valid biometry, if yes, validate, else, show error
fileprivate func biometricAuthentication(completion: @escaping ((Bool) -> ())){
    //        addBlurredBackground()
    //Check if device have Biometric sensor
    if biometryRetryCount == 2 {
        authenticationContext.localizedFallbackTitle = "Ok"
    } else {
        authenticationContext.localizedFallbackTitle = "Retry"
    }

    let isValidSensor : Bool = authenticationContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error)

    if isValidSensor {
        //Device have BiometricSensor
        authenticationContext.evaluatePolicy(
            .deviceOwnerAuthenticationWithBiometrics,
            localizedReason:  biometryRetryCount == 2 ? "You have been blocked from using the application for next 24 hours. Please come back later" : "Touch / Face ID authentication",
            reply: { [unowned self] (success, error) -> Void in

                if(success) {
                    // Touch or Face ID recognized
                    //                        self.removeBlurredBackground()
                    completion(true)
                } else {
                    //If not recognized then
                    if let error = error {
                        let msgAndAction = self.errorMessage(errorCode: error._code)
                        if msgAndAction.0 != "" {
                            UIApplication.topViewController()?.showAlert(withTitle: "Error", andMessage: msgAndAction.0, andActions: msgAndAction.1)
                        }
                    }
                    completion(false)
                }
        })
    } else {
        let msgAndAction = self.errorMessage(errorCode: (error?._code)!)
        if msgAndAction.0 != ""{
            UIApplication.topViewController()?.showAlert(withTitle: "Error", andMessage: msgAndAction.0, andActions: msgAndAction.1)
        }
    }

}

错误方法:

//MARK: TouchID error
fileprivate func errorMessage(errorCode:Int) -> (strMessage: String, action: [UIAlertAction]){

    var strMessage = ""
    let cancelAction = UIAlertAction.init(title:  Const.Localize.Common().kCancel, style: .cancel) { (cancelAction) in
    }
    var actions: [UIAlertAction] = [cancelAction]

    switch errorCode {

    case LAError.Code.authenticationFailed.rawValue:
        biometricAuthentication { (success) in
            //
        }
    case LAError.Code.userCancel.rawValue:
        if biometryRetryCount == 2 {
            blockUserFor24Hours()
        } else {
            showAlertOnCancelTapAction()
        }
    case LAError.Code.passcodeNotSet.rawValue:
        strMessage = "Please goto the Settings & Turn On Passcode"
    case LAError.Code.userFallback.rawValue:
        biometryRetryCount -= 2
        if biometryRetryCount == 0 {
            blockUserFor24Hours()
        } else {
            biometricAuthentication { (success) in
                //
            }
        }
    default:
        strMessage = evaluatePolicyFailErrorMessageForLA(errorCode: errorCode).strMessage
        actions = evaluatePolicyFailErrorMessageForLA(errorCode: errorCode).action
    }

    return (strMessage, actions)
}


func evaluatePolicyFailErrorMessageForLA(errorCode: Int) -> (strMessage: String, action: [UIAlertAction]){
    let cancelAction = UIAlertAction.init(title:  Const.Localize.Common().kCancel, style: .cancel) { (cancelAction) in
    }
    var actions: [UIAlertAction] = [cancelAction]

    var message = ""
    if #available(iOS 11.0, macOS 10.13, *) {
        switch errorCode {
        case LAError.biometryNotAvailable.rawValue:
            message = "Authentication could not start because the device does not support biometric authentication."

        case LAError.biometryLockout.rawValue:
            showPasscodeScreen()

        case LAError.biometryNotEnrolled.rawValue:
            message = "You do not have a registered biometric authentication. Kindly go to the settings and setup one"
            let settingsAction = UIAlertAction.init(title:  Const.Localize.Common().kSetting, style: .default) { (settingsAction) in
                UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)
            }
            actions.append(settingsAction)

        default:
            message = "Did not find error code on LAError object"
        }
    } else {
        switch errorCode {
        case LAError.touchIDLockout.rawValue:
            showPasscodeScreen()
        case LAError.touchIDNotAvailable.rawValue:
            message = "TouchID is not available on the device"
        case LAError.touchIDNotEnrolled.rawValue:
            message = "You do not have a registered biometric authentication. Kindly go to the settings and setup one"
            let settingsAction = UIAlertAction.init(title:  Const.Localize.Common().kSetting, style: .default) { (settingsAction) in
                UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)
            }
            actions.append(settingsAction)

        default:
            message = "Did not find error code on LAError object"
        }
    }

    return (message, actions)
}

0 个答案:

没有答案