iOS - Face ID生物识别集成

时间:2017-11-08 15:08:27

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

我已经为我的应用程序集成/实现了面部识别(本地身份验证)身份验证,除了面部识别提示警报窗口界面外,每件事情都可以正常工作。

  

它显示了一个圆角正方形,浅灰色背景和标题"面部   ID"

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

看看这张快照:

enter image description here

这是我的代码:

    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")
                    }
                }

            })
        })
    }

2 个答案:

答案 0 :(得分:11)

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

localizedReason仅适用于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& amp;的工作代码。 FaceID包含所有错误代码(Swift 4)

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

答案 1 :(得分:1)

在任何使用生物识别技术的项目中,请在应用程序的Info.plist文件中包含NSFaceIDUsageDescription键。没有此键,系统将不允许您的应用使用人脸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
        }
    }
}