[英]Can we test Face ID in simulator?
Simulator does not recognise a face but allows you to simulate a matching and non-matching faces, if you've enabled Enrolled
option from Face ID
. Simulator 无法识别人脸,但允许您模拟匹配和不匹配的人脸,
Enrolled
是您已从Face ID
启用Enrolled
选项。
Add following code to your view controller and try with Face-ID将以下代码添加到您的视图控制器并尝试使用 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 authentication will prompt you for first time to allow FaceID detection for your app. FaceID 身份验证将首次提示您允许对您的应用进行 FaceID 检测。
Now enable Face ID enrolment and run your app to test Face ID simulation Testing.现在启用 Face ID 注册并运行您的应用程序以测试 Face ID 模拟测试。
Here is simulation result for matching and non-matching faces.这是匹配和非匹配人脸的模拟结果。
Result for matching face:人脸匹配结果:
Result for non-matching face:不匹配人脸的结果:
The simulator just simulates the outcome of a correct and a failed face recognition, just like it does with Touch ID.模拟器只是模拟正确和失败的人脸识别结果,就像使用 Touch ID 一样。 It does not recognize faces .
它不识别面孔。
As you asking but after enabling that, what can I do?正如你所问,但启用后,我能做什么?
Just like a touch Id enrolment, You can verify things with face-Id on iPhone-X.就像触摸 ID 注册一样,您可以在 iPhone-X 上使用 face-Id 验证事物。 However simulator have some limitations like Appstore etc. With face-Id enrolment you can do following things -
但是模拟器有一些限制,如 Appstore 等。通过 face-Id 注册,您可以执行以下操作 -
same as give by @krunal just 2nd if should be outside of 1st.与@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
}
}
}
See this article.看到这篇文章。 You can create the Biometrics.m, Biometrics.h, and bridging-header.h files within you UITests folder and update your UI test target to use that bridging header.
您可以在 UITests 文件夹中创建 Biometrics.m、Biometrics.h 和 bridging-header.h 文件,并更新您的 UI 测试目标以使用该桥接头。 https://github.com/KaneCheshire/BiometricAutomationDemo
https://github.com/KaneCheshire/BiometricAutomationDemo
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.