iOS 11中的LABiometryType始终返回None

23

在此输入图片描述

无论设备的密码和TouchID设置如何配置,LAContext始终返回"none"。它只会抛出一个警告而不是异常。

只有在iOS11.1 beta中的XCode 9.1 Beta中才能正常工作,如下所示:(


在iPhone 7 Plus运行iOS 11和iPad Pro 12.9英寸上都是如此,但控制台日志显示为“[LAClient] initWithExistingContext -> (null),Error Domain=NSCocoaErrorDomain Code=4099 "The connection to service named com.apple.CoreAuthentication.daemon was invalidated from this process." UserInfo={NSDebugDescription=The connection to service named com.apple.CoreAuthentication.daemon was invalidated from this process.}"。 - guhan0
canEvaluatePolicy 在 iOS11 中正确地返回 True,evaluatePolicy 的效果也良好,可以提示用户使用 Touch ID。但是我收到了警告而不是异常 NSCocoaErrorDomain。 - guhan0
4个回答

37
我刚刚找到了问题所在!您需要调用canEvaluatePolicy来正确设置biometryType

例如:

func isFaceIdSupported() -> Bool {
    if #available(iOS 11.0, *){
        if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil) {
            return context.biometryType == LABiometryType.typeFaceID
        }
    }

    return false
}

根据苹果文档中关于biometryType的说明:
“仅在生物识别策略的canEvaluatePolicy(_:error:)成功时才设置此属性。默认值为none。”

4

我这里也遇到了同样的问题,通过以下代码进行了修复。但是仅适用于Xcode 9.1 Beta(在模拟器中使用iOS 11.1 beta)。

if (laContext.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: nil)) {

            if #available(iOS 11.0, *) {
                if (laContext.biometryType == LABiometryType.faceID) {
                    print("FaceId support")
                } else if (laContext.biometryType == LABiometryType.touchID) {
                    print("TouchId support")
                } else {
                    print("No Biometric support")
                }
            } else {
                // Fallback on earlier versions
            }
}

这不公平。希望苹果在 iPhone X 发布之前发布 XCode9.1。 - guhan0

4

如果您使用@Ermish的代码,如果设备上没有注册面部信息,则isFaceIdSupported()函数将返回假。 根据我最近在iOS SDK 11.1上的测试,只需调用laContext.canEvaluatePolicy函数并不关心结果,然后检查laContext.biometryType的内容即可。

如果没有注册面部信息,canEvaluatePolicy函数将失败,但是设备支持Face ID。


是的,重要的是您需要使用.canEvaluatePolicy(parameters)laContext的实例上设置.biometryType。我犯了一个错误,每次都使用新的实例LAContext()。 - Rickard Elimää

0
在Xamarin.iOS中,你需要在以下情况下评估策略:
   NSError error;
   bool success = context.CanEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, out error);
   if (context.BiometryType == LABiometryType.TouchId)
   {
       //Do Something
   }

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