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

为AppIntent设置认证策略未达预期效果

锁屏下App Intent无法触发认证的解决方案

问题背景

使用AppIntents框架创建的Intent已设置authenticationPolicy = .requiresLocalDeviceAuthentication,解锁状态下通过轻点背面/辅助触控触发快捷指令可正常运行,但锁屏状态下无响应——既不要求输入密码也不执行Intent。预期锁屏触发时需验证身份后显示Intent视图。

可能原因

  • 应用未配置本地认证权限描述,导致系统静默拒绝认证请求
  • 快捷指令在锁屏下的访问权限未开启
  • ShowsSnippetView返回类型在锁屏场景下的显示限制
  • Intent的静态认证策略在快捷指令上下文未正确生效

解决方案

1. 添加本地认证权限描述

在App的Info.plist中添加权限描述,这是系统要求的必要配置,否则认证请求会被拦截:

<key>NSLocalAuthenticationUsageDescription</key>
<string>验证身份以执行该操作</string>

如果使用面容ID,还需补充:

<key>NSFaceIDUsageDescription</key>
<string>使用面容ID验证身份</string>

2. 手动触发本地认证

在perform()方法中手动调用LocalAuthentication框架执行认证,确保锁屏下能唤起验证界面:

import AppIntents
import LocalAuthentication

struct SearchNumberIntent: AppIntent {
  static var title: LocalizedStringResource = "Sample App Intent"
  static var description = IntentDescription("Sample description")
  static var authenticationPolicy = IntentAuthenticationPolicy.requiresLocalDeviceAuthentication

  func perform() async throws -> some ShowsSnippetView {
    let context = LAContext()
    var authError: NSError?
    
    // 检查设备是否支持本地认证
    guard context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &authError) else {
      throw authError ?? NSError(domain: "AuthError", code: -1, userInfo: [NSLocalizedDescriptionKey: "无法执行身份验证"])
    }
    
    // 执行身份验证
    do {
      try await context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: "请验证身份以继续")
    } catch {
      throw error
    }
    
    return .result(
      dialog: "Sample Dialog",
      view: Text("Hello World")
    )
  }
}

3. 开启快捷指令锁屏访问权限

进入「设置」→「快捷指令」,确保「允许在锁定屏幕上访问」选项已开启,同时确认目标快捷指令未被设置为仅在解锁状态运行。

4. 调整Intent返回类型(可选)

锁屏下ShowsSnippetView的显示可能受系统限制,可尝试更换为ProvidesDialog类型测试:

func perform() async throws -> some ProvidesDialog {
  // 认证逻辑同上
  return .dialog("Sample Dialog")
}

5. 确认Intent注册配置

确保你的Intent已正确注册到App中:
在主App的Info.plist添加IntentsSupported数组:

<key>IntentsSupported</key>
<array>
  <string>SearchNumberIntent</string>
</array>

内容的提问来源于stack exchange,提问作者Srinivasan Rajendran

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 07:27:32