しめ鯖日記

swift, iPhoneアプリ開発, ruby on rails等のTipsや入門記事書いてます

LAContextのbiometryTypeがnoneになる問題

LAContextクラスのbiometryTypeプロパティーを使うと端末が指紋認証に対応しているか顔認証(FaceID)に対応しているかを判定する事ができます。

LAContext().biometryType == .faceID
LAContext().biometryType == .touchID

LAContextのbiometryTypeで起こった問題

しかし顔認証対応の端末で上のコードを実行してもtrueになりません。

LAContext().biometryType == .faceID // → false
LAContext().biometryType == .touchID // → false
LAContext().biometryType == .none // → true

LAContextのbiometryTypeで顔認証を判定する方法

判定するためには、canEvaluatePolicyメソッドを一度実行する必要があります。
こうすればfaceID対応している端末かを判定できます。

let context = LAContext()
var error: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
    print(context.biometryType == .faceID) // → true
}

ドキュメントの方にもcanEvaluatePolicyが呼ばれたタイミングで値がセットされると書かれています。

This property is set when canEvaluatePolicy has been called for a biometric policy.
The default value is LABiometryTypeNone.

指紋認証だとどうなるか

iPhone8など、指紋認証対応している端末で検証しても同じような結果になりました。

LAContext().biometryType == .faceID // → false
LAContext().biometryType == .touchID // → false
LAContext().biometryType == .none // → true