TouchID - 检测添加的新指纹 - evaluatedPolicyDomainState 何时更改?

TouchID - Detect new fingerprints added - When does evaluatedPolicyDomainState change?

我正在将 TouchID 集成到我的应用程序中。出于安全原因,我允许用户打开和关闭它。我希望它在用户添加新指纹时自动关闭。 根据 Apple 的说法,evaluatedPolicyDomainState

This property returns a value only when the canEvaluatePolicy(:error:) method succeeds for a biometric policy or the evaluatePolicy(:localizedReason:reply:) method is called and a successful Touch ID authentication is performed. Otherwise, nil is returned.

The returned data is an opaque structure. It can be used to compare with other values returned by this property to determine whether the database of authorized fingerprints has been updated. However, the nature of the change cannot be determined from this data.

但是,我添加了一个新指纹,evaluatedPolicyDomainState 保持不变。

关于如何确保 evaluatedPolicyDomainState 得到更新或者是否有任何其他方法检查是否添加了新指纹的任何想法?

所以经过几个小时的努力,我终于找到了解决办法。

    let context = LAContext()
    context.canEvaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics, error: nil)

    if let domainState = context.evaluatedPolicyDomainState
        where domainState == oldDomainState  {
        // Enrollment state the same

    } else {
        // Enrollment state changed

    }

每次添加或删除指纹时,域状态都会发生变化。您需要调用 canEvaluatePolicy 才能 evaluatedPolicyDomainState 进行更新。

下面是将evaluatedPolicyDomainState的数据值转换成字符串存储在keychain中的解决方案。如果Touch Id有变化,那么只需要比较evaluatedPolicyDomainState的值即可。

if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil) {
    if let domainState = context.evaluatedPolicyDomainState {
        let bData = domainState.base64EncodedData()
        if let decodedString = String(data: bData, encoding: .utf8) {
            print("Decoded Value: \(decodedString)")
        }
    }
}

注意:我没有针对 Face Id 测试此代码,我相信它对两者都适用。