LAContext().biometryType returns .none 在 iPhone X

LAContext().biometryType returns .none on iPhone X

我遇到了 CoreAuthentication 的问题。

我按照文档要求调用了 canEvaluatePolicy:error:,但结果始终是 .none

fileprivate let biometricsType: SecurityBiometrics = {
        var error: NSError?
        let evaluated = LAContext().canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: &error)
        if #available(iOS 11.0, *) {
            if LAContext().biometryType == .faceID { return .faceID }
            if LAContext().biometryType == .touchID { return .touchID }
        } else {
            if (evaluated || (error?.code != LAError.touchIDNotAvailable.rawValue)) {
                return .touchID
            }
        }
        return .none
    }()

// biometricsType returns `.none`

控制台上出现错误:

[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.}

它之前已经起作用了,但现在(没有任何变化)它仍然返回 .none

您是否运行遇到同样的错误?

您没有分享完整的错误信息。这是完整的错误消息:

[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.} 2018-03-29 13:42:37.866753+0530 Test[1505:35036] [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.}

用普通语言来说,它表示您的 LAContext() 有问题,它在您的代码块中每次 ([LAClient] initWithExistingContext -> (null)) 都被初始化。使用 LAContext 的单个实例。

试试看:

fileprivate let biometricsType: LABiometryType = {
    let laContext = LAContext()
    var error: NSError?
    let evaluated = laContext.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: &error)

    if let laError = error {
        print("laError - \(laError)")
        return .none
    }

    if #available(iOS 11.0, *) {
        if laContext.biometryType == .faceID { return .faceID }
        if laContext.biometryType == .touchID { return .touchID }
    } else {
        if (evaluated || (error?.code != LAError.touchIDNotAvailable.rawValue)) {
            return .touchID
        }
    }
    return .none
}()

// print biometricsType
print("biometricsType - \(biometricsType.rawValue)")

结果:biometricsType - 2

看看这个快照: