App Intents — App Intents
This article is extracted from the chat log with AI. Please identify it with caution.

下面我将系统性、成体系地为你介绍 App Intents 的使用方式,这是 Apple 在 iOS 16 引入的新框架,用于替代旧的 IntentsSiriKit 机制,旨在更现代、更轻量地将你的 App 与 Siri、聚焦搜索、快捷指令、App Shortcuts 等深度集成。


🧠 一、App Intents 是什么? #

App Intents Framework 允许开发者以声明式方式将 App 功能公开给:

  • Siri

  • Spotlight 搜索

  • 快捷指令(Shortcuts app)

  • App Shortcuts(无需用户添加的自动可见快捷方式)

  • 交互式小组件(如 iOS 17+ 的主屏幕 Widget)

  • Live Activities(可用作交互入口)

简单说,App Intents 是 iOS 上的“可编程接口 + UI 展示”的组合,让 App 能被系统调用和用户自动触达。


🧱 二、App Intents 的结构组成 #

1. AppIntent #

实现一个操作功能(比如启动计时器、添加待办事项、控制灯光)

2. AppShortcutsProvider #

可选,实现后系统会自动将快捷操作显示在 Spotlight、Siri Suggestions、锁屏 Siri 推荐等位置

3. 参数类型 #

支持 String, Int, Bool, Double, Date, URL, Enum, AppEntity(用于识别 App 中的对象)


✅ 三、实现 AppIntent 的基本流程 #

以“开始一个计时器”为例:


第一步:新建 StartTimerIntent.swift #

import AppIntents

struct StartTimerIntent: AppIntent {
    static var title: LocalizedStringResource = "Start Timer"
    static var description = IntentDescription("Start a focus timer for a number of minutes.")

    // 输入参数
    @Parameter(title: "Minutes")
    var minutes: Int

    // 实现入口
    func perform() async throws -> some IntentResult {
        // 启动你自己的逻辑
        TimerController.shared.startTimer(for: minutes)
        return .result(dialog: "Started a timer for \(minutes) minutes.")
    }
}

说明:

  • @Parameter 定义用户可以配置的输入项

  • perform() 中实现你想做的事情(可以调用你的模型、更新数据、启动 Live Activity 等)

  • IntentResult 支持 dialog、result、navigation、widget 交互等


第二步:创建快捷方式定义(可选) #

如果你希望系统能自动显示 “开始计时器” 快捷方式:

import AppIntents

struct TimerShortcutProvider: AppShortcutsProvider {
    static var appShortcuts: [AppShortcut] {
        AppShortcut(
            intent: StartTimerIntent(minutes: 25),
            phrases: [
                "Start a timer for \(.parameter(\.minutes)) minutes",
                "Focus for \(.parameter(\.minutes)) minutes"
            ],
            shortTitle: "Start Timer",
            systemImageName: "timer"
        )
    }
}

这样系统会在 Spotlight、Siri、快捷指令建议中自动列出该操作。


📱 四、让 Widget 使用 App Intents(iOS 17+) #

Button(intent: StartTimerIntent(minutes: 10)) {
    Text("Start 10-min Focus")
}

这就是 iOS 17 交互式 Widget 的入口机制,需要用 AppIntent 作为 action。


🧪 五、测试方法 #

  • 快捷指令 App:点击右上角 + 添加操作,选择你的 App 提供的 Intent

  • Siri 语音:说出你定义的 phrase,比如“Start a timer for 25 minutes”

  • Widget 按钮:使用 .buttonStyle(.borderedProminent) 的按钮调用 intent

  • 聚焦搜索:搜索 App 提供的快捷方式


🧩 六、进阶功能(可选) #

1. 自定义实体(AppEntity) #

用于表示你的 App 中的某类对象,比如“任务”、“事件”、“联系人”等。

struct Task: AppEntity {
    var id: String
    var title: String

    static var typeDisplayRepresentation: TypeDisplayRepresentation {
        "Task"
    }

    var displayRepresentation: DisplayRepresentation {
        DisplayRepresentation(title: "\(title)")
    }
}

然后可以在 Intent 中传入该实体:

@Parameter(title: "Which Task")
var task: Task

2. 支持导航结果(iOS 17+) #

return .result(navigation: .value(destination: .timerDetailView))

允许你通过 Intent 引导用户导航到 App 的某个页面。


✅ 七、完整示例:为计时器添加 AppIntent #

1. Intent 定义 #

struct StartFocusTimerIntent: AppIntent {
    static var title: LocalizedStringResource = "Start Focus Timer"

    @Parameter(title: "Minutes")
    var duration: Int

    func perform() async throws -> some IntentResult {
        TimerController.shared.startTimer(for: duration)
        return .result(dialog: "Started a focus timer for \(duration) minutes.")
    }
}

2. 快捷方式定义 #

struct FocusShortcuts: AppShortcutsProvider {
    static var appShortcuts: [AppShortcut] {
        [
            AppShortcut(
                intent: StartFocusTimerIntent(duration: 25),
                phrases: [
                    "Start a focus timer for \(.parameter(\.duration)) minutes"
                ],
                shortTitle: "Start 25m Timer",
                systemImageName: "clock"
            )
        ]
    }
}

📌 总结 #

特性说明
框架AppIntents(iOS 16+)
输入支持结构化参数(自动 UI 表单)
输出支持语音反馈、导航、UI 更新
集成点Siri、快捷方式、Spotlight、小组件、Live Activity
使用方式实现 AppIntent 协议、提供参数和逻辑
推荐版本iOS 17+ 体验最佳,支持 Widget 按钮调用 Intent
本文共 1229 字,创建于 May 19, 2025
相关标签: Xcode, SwiftUI, ByAI, ChatGPT