[英]Can we test Face ID in simulator?
Simulator 无法识别人脸,但允许您模拟匹配和不匹配的人脸, Enrolled
是您已从Face ID
启用Enrolled
选项。
将以下代码添加到您的视图控制器并尝试使用 Face-ID
import LocalAuthentication
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
localAuthentication()
}
func localAuthentication() -> Void {
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, *) {
if (laContext.biometryType == LABiometryType.faceID) {
localizedReason = "Unlock using Face ID"
print("FaceId support")
} else if (laContext.biometryType == LABiometryType.touchID) {
localizedReason = "Unlock using Touch ID"
print("TouchId support")
} else {
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")
}
}
})
})
}
}
}
FaceID 身份验证将首次提示您允许对您的应用进行 FaceID 检测。
现在启用 Face ID 注册并运行您的应用程序以测试 Face ID 模拟测试。
这是匹配和非匹配人脸的模拟结果。
人脸匹配结果:
不匹配人脸的结果:
模拟器只是模拟正确和失败的人脸识别结果,就像使用 Touch ID 一样。 它不识别面孔。
正如你所问,但启用后,我能做什么?
就像触摸 ID 注册一样,您可以在 iPhone-X 上使用 face-Id 验证事物。 但是模拟器有一些限制,如 Appstore 等。通过 face-Id 注册,您可以执行以下操作 -
与@krunal 给出的相同,如果应该在第一之外,则为第二。
import LocalAuthentication
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
localAuthentication()
}
func localAuthentication() -> Void {
let laContext = LAContext()
var error: NSError?
let biometricsPolicy = LAPolicy.deviceOwnerAuthenticationWithBiometrics
if (laContext.canEvaluatePolicy(biometricsPolicy, error: &error)) {
var localizedReason = "Unlock device"
if #available(iOS 11.0, *) {
if (laContext.biometryType == LABiometryType.faceID) {
localizedReason = "Unlock using Face ID"
print("FaceId support")
} else if (laContext.biometryType == LABiometryType.touchID) {
localizedReason = "Unlock using Touch ID"
print("TouchId support")
} else {
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")
}
}
})
})
}
//This should be outside of if
if let laError = error {
print("laError - \(laError)")
return
}
}
}
看到这篇文章。 您可以在 UITests 文件夹中创建 Biometrics.m、Biometrics.h 和 bridging-header.h 文件,并更新您的 UI 测试目标以使用该桥接头。 https://github.com/KaneCheshire/BiometricAutomationDemo
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.