我运行了最新的Xcode 9 GM(2017年9月13日),并在模拟器和Hardware > Face ID > Enrolled
中设置了Deployment Target 11.0
。但是我收到错误代码-6 LAErrorTouchIDNotAvailable
。
我缺少一些设置吗?
let myContext = LAContext()
let myLocalizedReasonString = "You are pretty"
var authError: NSError?
if #available(iOS 8.0, macOS 10.12.1, *) {
if myContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError) {
myContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: myLocalizedReasonString) { success, evaluateError in
if success {
print("// User authenticated successfully, take appropriate action")
} else {
print(" // User did not authenticate successfully, look at error and take appropriate action")
}
}
} else {
print(" // Could not evaluate policy; look at authError and present an appropriate message to user")
}
} else {
print(" // Fallback on earlier versions")
}
答案 0 :(得分:8)
由于框架错误,Face ID在Xcode 9 GM中无效。 Xcode 9.1解决了这个问题。
您可以在iPhone 8模拟器中测试您的应用,并确保其在Touch ID上正常运行或运行Xcode 9.1 beta并在那里测试Face ID支持。
答案 1 :(得分:5)
我认为iphone X模拟器的faceID目前无法正常工作,希望他们能尽快解决这个问题......
https://forums.developer.apple.com/thread/86779
我们可以做一个错误报告,看看它是否能加快速度:P https://developer.apple.com/bug-reporting
答案 2 :(得分:3)
Face ID现在正在运行,使用Xcode 9.1。请按照以下步骤在Simulator中进行测试。
在目标的info.plist文件中添加隐私声明。
将LocalAuthentication
框架导入您的项目并将以下代码添加到您的视图控制器并尝试使用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模拟测试。
以下是匹配和不匹配面的模拟结果。
匹配面孔的结果:
不匹配面孔的结果:
答案 3 :(得分:1)
XCode 9.1 beta今天发布,其中原始代码应该在模拟器中完美运行!
答案 4 :(得分:1)
根据LAContext的Apples文档,我们需要添加密钥NSFaceIDUsageDescription
,其原因是使用String,因为它将显示在设备上使用FaceId的授权请求。
示例将此添加到info.plist:
NSFaceIDUsageDescription
将其设置为String类型,并在访问Face ID摄像头的提示请求中添加要显示的文本。
"Your app" request your permission to use Face ID, for you to login to your account / unlock your notes / what ever reason in the end.
通过添加此功能,您可以转到iPhone X的模拟器,系统将提示您输入面部识别码,按下接受,一切都应该完美。
请记住,通过进入,为模拟器注册生物识别支持
Simulator -> Hardware -> Face ID / Touch ID -> Enrolled
然后你只需要按Match / Non-Matching Touch / Face ID
来测试你的处理
有关详细信息,请查看Apple的文档:https://developer.apple.com/documentation/localauthentication/lacontext
----编辑----
这在Xcode 9.0和9.1中都适用于我