用途:这份资料介绍 Apple 平台的 AppIntents 框架,目标是把应用能力接入 Siri、快捷指令(Shortcuts)、Spotlight 与 Visual Intelligence。它面向需要为应用增加系统级意图与搜索入口的 Swift 开发者。
主要能力:
- 基础意图:定义遵循 AppIntent 协议的结构体,用 @Parameter 声明参数,在 perform() 中返回结果。
- 意图模式:通过 supportedModes 控制前后台执行,如 [.background, .foreground]、[.background, .foreground(.dynamic)]、[.background, .foreground(.deferred)],并可用 continueInForeground(alwaysConfirm: false) 请求前台。
- 属性宏:@ComputedProperty 从数据源实时读取,@DeferredProperty 按需异步拉取昂贵数据。
- Spotlight 集成:实体同时遵循 AppEntity 与 IndexedEntity,提供 searchableAttributes,并用 CSSearchableIndex.default().indexAppEntities(landmarks, priority: .normal) 建立索引,用 deleteAppEntities(identifiedBy:ofType:) 移除。
- 交互式片段:SnippetIntent 提供 snippet 视图,可包含按钮等交互元素。
- App Shortcuts:通过 AppShortcutsProvider 注册短语与系统图标。
- Swift 包支持:实现 AppIntentsPackage,并用 includedPackages 聚合依赖包中的意图。
关键约束:不要使用旧的 SiriKit INIntent,改用 AppIntent 协议;Spotlight 不要只依赖 NSUserActivity,应使用 IndexedEntity 加 CSSearchableIndex 索引;不要写死仅前台意图,应通过 supportedModes 灵活配置。
参考:App Intents 官方文档与 WWDC 2025 相关演讲。具体 API 行为、可用系统版本与完整示例暂未提供。