在调用 LAContext.evaluatePolicy
后,是否可能以编程方式取消 TouchID 提示框? 如果是,怎么做?
并非所有Apple发布的API都会出现在developer.apple.com(或Xcode文档查看器)的文档中。API差异列表列出了公共API,因此您在那里看到的任何内容都在头文件(请参见LocalAuthentication/LAContext.h
)和从这些头文件生成的Swift接口中。而任何在头文件中的内容都是公共API,因此您可以自由调用它。
有时(但并不总是),未记录的API具有体面的头注释,说明如何使用它们...值得庆幸的是,LAContext.invalidate()
就是其中之一:
/// Invalidates the context.
///
/// @discussion The context is invalidated automatically when it is (auto)released. This method
/// allows invalidating it manually while it is still in scope.
///
/// Invalidation terminates any existing policy evaluation and the respective call will
/// fail with LAErrorAppCancel. After the context has been invalidated, it can not be
/// used for policy evaluation and an attempt to do so will fail with LAErrorInvalidContext.
///
/// Invalidating a context that has been already invalidated has no effect.
@available(iOS 9.0, *)
public func invalidate()
实际上,当 Touch ID 提示框出现时调用 invalidate()
方法应该会将其关闭。(我个人没有尝试过。)
iOS 11 更新:请注意,在使用 Face ID 而非 Touch ID 的设备上,当你调用 LAContext.evaluatePolicy
方法时出现的提示/ HUD 样式 UI 不需要或不允许交互,并且在成功认证后自动关闭。理论上,invalidate
调用仍然可以关闭这个提示框(或者如果 Face ID 无法识别用户,则会出现实际互动的后续提示框)。
但是,假设你不能保证在要求身份验证之后始终有足够的时间来取消 LAContext
的身份验证,那么这样做可能并不明智。
invalidate()
来解除。我做错了什么? :) - hfosslipublic class MyBiometryUtility: NSObject {
static private var sharedBiometry: MyBiometryUtility? = nil
var context: LAContext = LAContext()
@objc public static func sharedInstance() -> MyBiometryUtility{
if let sharedBiometry = sharedBiometry {
return sharedBiometry;
} else{
sharedBiometry = MyBiometryUtility()
return sharedBiometry!
}
}
public func tryBiometryAuth() { ... }
public func closeBiometryAuth() {... }
在SomeViewController.swift文件中
func buttonTapped(sender: Any){
// show dialog.
MyBiometryUtility.sharedInstance().tryBiometryAuth()
}
func timerCountDown(){
// close dialog.
if tooLong() {
MyBiometryUtility.sharedInstance().closeBiometryAuth()
}
}
LAContext.evaluatePolicy
会打开一个提示框,要求用户使用 TouchID。我想从我的代码中关闭这个对话框。例如,在几秒钟后,如果用户没有采取任何行动。 - Sven-Michael StübeLAContext invalidate
方法,不确定它是否足以被视为公共方法。 - jcaron