2017-09-13 216 views
5

我已经运行了最新的Xcode 9 GM(2017年9月13日),并在模拟器中设置了Hardware > Face ID > Enrolled以及Deployment Target 11.0。但是,我收到错误代码-6 LAErrorTouchIDNotAvailableiOS 11模拟器不支持LAContext和FaceID

有一些设置我错过了吗?

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

使用这个库,它支持faceid和touchid都。 https://github.com/tejas-ardeshna/TJBioAuthentication –

回答

8

由于框架错误,Face ID在Xcode 9 GM中不起作用。 Xcode 9.1修复了这个问题。

您可以在iPhone 8模拟器中测试您的应用程序,并确保它可以正确使用Touch ID或运行Xcode 9.1测试版并在其中测试Face ID支持。

1

今天XCode 9.1 beta出来了,其中原代码应该在模拟器中完美运行!

1

根据LAContext的Apples文档,我们需要添加密钥NSFaceIDUsageDescription以及使用String的原因,因为这将显示在设备上使用FaceId的授权请求。

实施例添加这info.plist中:

NSFaceIDUsageDescription 

把它的类型字符串,并添加要被示出的文本中,在该提示请求用于访问面部识别照相机。

"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,来测试你的处理

有关详细信息,并检查了苹果的文档:https://developer.apple.com/documentation/localauthentication/lacontext

----编辑----

这对Xcode 9.0和9.1都适用我

2

Face ID现在正在使用Xcode 9.1。按照以下步骤在Simulator中进行测试。

将隐私声明添加到您目标的info.plist文件中。

enter image description here

导入LocalAuthentication框架到您的项目和下面的代码添加到您的视图控制器,并与FaceID尝试

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检测您的应用程序。

enter image description here


现在启用面部识别注册并运行应用程序来测试面部识别模拟测试。

enter image description here

这里是用于匹配和非匹配面仿真结果。

结果匹配的脸:

enter image description here


结果为不匹配的脸:

enter image description here

+1

它工作得很好 –