10

我已经为我的应用程序集成/实现了人脸 ID(本地身份验证)身份验证,一切正常,除了人脸 ID 提示警报窗口界面。

它显示了一个带有浅灰色背景和标题“Face ID”的圆角正方形

需要为标题上方的空白区域设置什么?是人脸ID图标的空间吗?如果是,那么我该如何设置?我已经尝试了 LAContext 和 LAPolicy 中的所有内容。

看这个快照:

在此处输入图像描述

这是我的代码:

    let laContext = LAContext()
    var error: NSError?
    let biometricsPolicy = LAPolicy.deviceOwnerAuthenticationWithBiometrics

    if (laContext.canEvaluatePolicy(biometricsPolicy, error: &error)) {

        if let laError = error {
            print("laError - \(laError)")
            return
        }

        var localizedReason = "Unlock device"
        if #available(iOS 11.0, *) {
            switch laContext.biometryType {
            case .faceID: localizedReason = "Unlock using Face ID"; print("FaceId support")
            case .touchID: localizedReason = "Unlock using Touch ID"; print("TouchId support")
            case .none: print("No Biometric support")
            }
        } else {
            // Fallback on earlier versions
        }


        laContext.evaluatePolicy(biometricsPolicy, localizedReason: localizedReason, reply: { (isSuccess, error) in

            DispatchQueue.main.async(execute: {

                if let laError = error {
                    print("laError - \(laError)")
                } else {
                    if isSuccess {
                        print("sucess")
                    } else {
                        print("failure")
                    }
                }

            })
        })
    }
4

2 回答 2

21

这仅在模拟器中发生,在实际设备中,画布被面部图标动画占据。

本地化原因仅适用于 Touch ID,因为它们都共享相同的 API。

更新 1:添加了屏幕录像:

他们都运行相同的代码:

func beginFaceID() {

    guard #available(iOS 8.0, *) else {
        return print("Not supported")
    }

    let context = LAContext()
    var error: NSError?

    guard context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) else {
        return print(error)
    }

    let reason = "Face ID authentication"
    context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { isAuthorized, error in
        guard isAuthorized == true else {
            return print(error)
        }

        print("success")
    }

}

这是带有所有错误代码的 TouchID 和 FaceID 的工作代码(Swift 4)

https://stackoverflow.com/a/52093551/10150796

于 2017-11-09T14:50:16.247 回答
3

在任何使用生物识别的项目中,将NSFaceIDUsageDescription键包含在您的应用Info.plist文件中。如果没有此密钥,系统将不允许您的应用使用 Face ID。

let authContext = LAContext()
        authContext.localizedFallbackTitle = "Use Passcode"
        authContext.localizedCancelTitle = "Cancel"

var authError: NSError?

if authContext.canEvaluatePolicy(.deviceOwnerAuthentication, error: &authError) {
   evaluatePolicy(policy, context: authContext)
} else {
     guard let error = authError else { return }
     print("Error: \(error.code)")
     checkError(error)
}

private func evaluatePolicy(_ policy: LAPolicy, context: LAContext) {
        context.evaluatePolicy(policy, localizedReason: reason) { (success, error) in
            if success {
                print("Success")

            } else {
                guard let error = error else { return }
                self.checkError(error as NSError)
            }
        }
}

private func checkError(_ error: NSError) {
    guard let error = error as? LAError else { return }

    switch error.code {
    case .authenticationFailed:
        print("authenticationFailed")
        requestAuth(policy: .deviceOwnerAuthentication)
    case .userFallback:
        print("userFallback")
        requestAuth(policy: .deviceOwnerAuthentication)
    case .userCancel:
        print("userCancel")
    case .systemCancel:
        print("systemCancel")
    case .passcodeNotSet:
        print("passcodeNotSet")
    case .appCancel:
        print("appCancel")
    case .invalidContext:
        print("invalidContext")
    case .notInteractive:
        print("notInteractive")
    default:
        checkBioMetricError(error)
    }
}

private func checkBioMetricError(_ error: LAError) {
    if #available(iOS 11.0, *) {

        switch error.code {
        case .biometryNotAvailable,
             .biometryNotEnrolled,
             .biometryLockout:
            requestAuth(policy: .deviceOwnerAuthentication)

        default: break
        }
    } else {
        switch error.code {

        case .touchIDNotAvailable,
             .touchIDNotEnrolled,
             .touchIDLockout:
            requestAuth(policy: .deviceOwnerAuthentication)

        default: break
        }
    }
}
于 2019-09-30T06:42:19.600 回答