Cri*_*ena 14 ios touch-id swift
我有许多地方描述的LocalAuthentication的以下实现.
context.evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: "Logging in with Touch ID", reply: { (success : Bool, error : NSError? ) -> Void in
dispatch_async(dispatch_get_main_queue(), {
if success {
let alert = UIAlertController(title: "Success", message: "", cancelButtonTitle: "Great!")
self.presentViewController(alert, animated: true, completion: nil)
}
if let error = error {
var message :String
switch(error.code) {
case LAError..AuthenticationFailed:
message = "There was a problem verifying your identity."
case LAError..UserCancel:
message = "You pressed cancel."
case LAError..UserFallback:
message = "You pressed password."
default:
message = "Touch ID may not be configured"
}
let alert = UIAlertController(title: "Error", message: message, cancelButtonTitle: "Darn!")
self.presentViewController(alert, animated: true, completion: nil)
}
})
})
Run Code Online (Sandbox Code Playgroud)
但在我使用我的指纹成功验证后,evaluatePolicy(,localizedReason:,reply :)返回成功而不请求任何指纹.我实际上正在使用UISwitch启用或禁用TouchID,因此在禁用和重新启用后,我想重新进行身份验证并重新输入我的指纹.
为什么要缓存身份验证?
谢谢
Cri*_*ena 24
LAContext一经评估,将在取消分配之前返回成功.您可以手动使其失效,然后返回的错误将是LAError.InvalidContext.
如果您希望每次都提示TouchID确认,则需要每次都创建一个LAContext.这可以实现
context.evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: "Logging in with Touch ID", reply: { (success : Bool, error : NSError? ) -> Void in
dispatch_async(dispatch_get_main_queue(), {
if success {
let alert = UIAlertController(title: "Success", message: "", cancelButtonTitle: "Great!")
self.presentViewController(alert, animated: true, completion: nil)
}
if let error = error {
var message :String
switch(error.code) {
case LAError..AuthenticationFailed:
message = "There was a problem verifying your identity."
case LAError..UserCancel:
message = "You pressed cancel."
case LAError..UserFallback:
message = "You pressed password."
default:
message = "Touch ID may not be configured"
}
let alert = UIAlertController(title: "Error", message: message, cancelButtonTitle: "Darn!")
self.presentViewController(alert, animated: true, completion: nil)
}
context = LAContext()
})
})
Run Code Online (Sandbox Code Playgroud)
从ios 9开始就有touchIDAuthenticationAllowableReuseDuration了背景
允许使用Touch ID身份验证的持续时间.如果在指定的时间间隔内使用Touch ID成功验证设备,则接收器的验证会自动成功,而不会提示用户输入Touch ID.默认值为0,表示无法重复使用Touch ID身份验证.Touch ID身份验证重用的最大允许持续时间由LATouchIDAuthenticationMaximumAllowableReuseDuration常量指定.通过将此属性设置为大于此常量的值,您无法指定更长的持续时间.可用性iOS(9.0及更高版本),macOS(10.12及更高版本)
如果你设置为例如60
context.touchIDAuthenticationAllowableReuseDuration = 60
Run Code Online (Sandbox Code Playgroud)
如果用户在过去60秒内成功通过了触摸ID检查,它将自动成功而不进行检查.
因此,您可以设置适合您的值.我发现它非常好并且让用户在几秒钟之前再次触摸时再次触摸是很烦人的.(例如解锁屏幕).
| 归档时间: |
|
| 查看次数: |
3739 次 |
| 最近记录: |