2016-07-15 33 views
1

我正在将TouchID集成到我的应用中。出于安全原因,我允许用户打开和关闭它。我希望它在用户添加新指纹时自动关闭。 据苹果,evaluatedPolicyDomainStateTouchID - 检测添加的新指纹 - evaluatePolicyDomainState何时更改?

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得到更新或者是否有任何其他方式检查是否添加新指纹?

回答

8

所以经过几个小时的挣扎之后,我终于找到了解决方案。

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

+0

嗨克里斯蒂安,我们也有这个requiremnet在我们的app.Can你请告诉我应该分配给oldDomainState变量? – RXGangam

+0

当您第一次请求用户设置touchID时,您将使用'context.evaluatePolicy'。如果成功,则获取当前策略并将其保存到oldDomainState。 –

+0

我已使用kSecAccessControlTouchIDCurrentSet。现在它按预期工作。 – RXGangam

0

下面是将evaluatePolicyDomainState的数据值转换为字符串并将其存储在钥匙串中的解决方案。如果Touch ID有任何变化,那么您只需比较评估策略域名状态的值。

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

注:我没有测试的面部识别验证码,我相信它会为这两个工作。