2017-09-13 92 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

它工作得很好 –