You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在不提示用户的情况下检测iOS设置中是否禁用了App的Face ID?

静默检测iOS应用Face ID权限状态的方案

你之前的思路其实走对了一半——canEvaluatePolicy方法本身不会触发用户认证提示,它只是用来评估当前策略是否可执行,刚好能满足你静默检测的需求。问题出在你没有正确解析返回的错误码,没法区分「应用被禁用Face ID」和其他生物识别不可用的情况。

核心判断逻辑

调用context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error)后,通过错误码可以精准判断权限状态:

  • 返回true:应用拥有Face ID权限,且设备支持、用户已录入
  • 返回false时,根据LAError的code区分:
    • .biometryNotAllowed:用户在系统设置中专门禁用了本应用的Face ID权限(这正是你要检测的状态)
    • .biometryNotAvailable:设备本身不支持Face ID
    • .biometryNotEnrolled:用户未录入Face ID信息

修改后的检测代码

import LocalAuthentication

private let context = LAContext()

private func checkFaceIDPermissionStatus() -> FaceIDPermissionStatus {
    var error: NSError?
    
    let canEvaluate = context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error)
    
    guard canEvaluate else {
        guard let laError = error as? LAError else {
            return .unknown
        }
        
        switch laError.code {
        case .biometryNotAllowed:
            return .appPermissionDisabled
        case .biometryNotAvailable:
            return .deviceNotSupport
        case .biometryNotEnrolled:
            return .noFaceIDEnrolled
        default:
            return .unknown
        }
    }
    
    return .permissionGranted
}

// 定义枚举方便状态管理
enum FaceIDPermissionStatus {
    case permissionGranted
    case appPermissionDisabled
    case deviceNotSupport
    case noFaceIDEnrolled
    case unknown
}

从设置返回后更新UI

要监听应用从后台回到前台的事件,在页面初始化时注册通知:

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(updateFaceIDStatusUI), name: UIApplication.didBecomeActiveNotification, object: nil)
}

@objc private func updateFaceIDStatusUI() {
    let status = checkFaceIDPermissionStatus()
    // 根据状态更新UI,比如开关状态、提示文本
    switch status {
    case .permissionGranted:
        faceIDToggle.isOn = true
    case .appPermissionDisabled:
        faceIDToggle.isOn = false
    case .deviceNotSupport, .noFaceIDEnrolled:
        faceIDToggle.isEnabled = false
        statusLabel.text = "Face ID不可用"
    default:
        statusLabel.text = "状态未知"
    }
}

// 记得在页面销毁时移除通知
deinit {
    NotificationCenter.default.removeObserver(self)
}

注意事项

  • 每次检测前可以新建LAContext实例,避免缓存旧的权限状态
  • iOS 11及以上版本支持.biometryNotAllowed错误码,低版本可兼容处理LAError.passcodeNotSet等情况

内容的提问来源于stack exchange,提问作者Tornike Gomareli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 10:42:50