iOS 11模拟器无法使用LAContext和FaceID。

7

我正在使用最新的Xcode 9 GM(2017年9月13日),并在模拟器中设置了硬件 > 面容ID > 已注册以及部署目标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")
}

使用这个库,它支持FaceID和TouchID两种技术。https://github.com/tejas-ardeshna/TJBioAuthentication - Tejas Ardeshna
5个回答

7

由于框架bug,Face ID无法在Xcode 9 GM中正常工作。但是,Xcode 9.1可以解决这个问题。

您可以在iPhone 8模拟器中测试您的应用程序,并确保它可以正确地与Touch ID配合使用,或者运行Xcode 9.1 beta并在那里测试Face ID的支持。


5

4

现在,Face ID已经可以使用了,使用Xcode 9.1即可。按照以下步骤在模拟器中测试它。

在你的目标信息属性列表(info.plist)文件中添加隐私声明。

enter image description here

导入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模拟测试。以下是匹配和不匹配脸部的模拟结果。匹配脸部的结果如下所示:非匹配脸部的结果如下所示:

1
根据苹果公司的 LAContext 文档,我们需要添加键 NSFaceIDUsageDescription,并提供使用原因字符串,这将显示在设备上使用 FaceId 的授权请求。例如,将其添加到 info.plist 文件中:
NSFaceIDUsageDescription

将其设置为字符串类型,并在请求访问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模拟器,并会提示你进行面容ID,按接受即可,一切都应该完美运作。
记得通过进入模拟器 -> 硬件 -> 面容ID / 触控ID -> 已注册来为模拟器注册生物识别支持。
然后你只需要按下匹配 / 不匹配触摸 / 面容ID来测试你的处理方式。
了解更多详情并查看苹果文档,请访问https://developer.apple.com/documentation/localauthentication/lacontext ---- 编辑 ----
在Xcode 9.0和9.1中,这对我都起作用。

1
今天发布了XCode 9.1 beta版,原始代码在模拟器中应该能够完美运行!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接