【iOS小组件】Siri和快捷指令

776 阅读3分钟

严格上说快捷指令并不属于小组件,只是和小组件一样通过  AppIntent 意图来定义,这里为了归类特意放到了一起。

快捷指令


苹果的快捷指令(Shortcuts)是一种自动化工具,它允许用户通过创建自定义的“快捷指令”来自动化一系列任务。这些任务可以是简单的单个动作,比如设置一个提醒,也可以是复杂的多步骤操作,比如发送带有特定照片的邮件等。

image.png

没有了解过的可以先了解一下,以后有时间再玩。


创建快捷指令


AppItent 意图创建快捷指令时默认状态是可见的,也就是 isDiscoverable 的值true,当意图被定义和注册后,在快捷指令App中添加操作时便可以找到该意图。添加之后,可以在快捷指令App中手动点击执行,也可以通过 Siri 执行。

注意:

**快捷指令需要 iOS 16 及以上系统版本
**

image.png

iOS 16之后,可以实现 AppShortcutsProvider 协议,向快捷指令App添加 AppShortcut 无需手动添加就可以在快捷指令App里看到对应的快捷指令

import AppIntents

struct MeditationShortcuts: AppShortcutsProvider {
    static var appShortcuts: [AppShortcut] { }
}

1.单指令

import AppIntents

struct StartMeditationIntent: AppIntent {
    static let title: LocalizedStringResource = "Start Meditation Session"

    func perform() async throws -> some IntentResult & ProvidesDialog {
        return .result(dialog: "Okay, starting a meditation session.")
    }
}

struct MeditationShortcuts: AppShortcutsProvider {
    static var appShortcuts: [AppShortcut] {
        AppShortcut(
            intent: StartMeditationIntent(),
            phrases: [
                "\(.applicationName) shortcut1",
            ],
            shortTitle: "shortcut1",
        )
    }
}

AppShortcut 参数:

  • intent:定义了快捷方式要执行的动作,当用户触发这个快捷方式时,应用将执行该 Intentperform()

  • phrases:这是一个字符串数组,定义了用户可以通过 Siri 触发这个快捷方式时使用的短语, 实验证明短语中不包含 applicationName 快捷方式将不被展示。

  • shortTitle:这是一个简短的标题,用于在快捷方式的用户界面上显示(暂时没看到在哪展示)

在快捷指令【所有快捷指令】中找到快捷指令,点击快捷指令可以触发执行,预览效果如下:

image.png

2.多指令

注意:多指令模式最多只能添加 10条 指令

import AppIntents

struct StartMeditationIntent: AppIntent {
    static let title: LocalizedStringResource = "Start Meditation Session"

    func perform() async throws -> some IntentResult & ProvidesDialog {
        return .result(dialog: "Okay, starting a meditation session.")
    }
}

struct MeditationShortcuts: AppShortcutsProvider {
    static var appShortcuts: [AppShortcut] {
        AppShortcut(
            intent: StartMeditationIntent(),
            phrases: [
                "\(.applicationName) shortcut1",
            ],
            shortTitle: "shortcut1"
        )

        AppShortcut(
            intent: StartMeditationIntent(),
            phrases: [
                "\(.applicationName) shortcut2",
            ],
            shortTitle: "shortcut2"
        )
    }
}

在快捷指令【所有快捷指令】中找到快捷指令执行,预览效果如下:

image.png

多指令有数量限制,最多添加 10条,超过 10条会报错。

image.png

3.指令图标

可以为快捷指令添加图标,以便快速查找指令。

import AppIntents

struct AddTodoIntent: AppIntent {
    static var title: LocalizedStringResource = "Add Todo"

    func perform() async throws -> some IntentResult & ProvidesDialog {
        // 实现添加待办事项的逻辑
        .result(dialog: "New todo added successfully.")
    }
}

struct TodoAppShortcuts: AppShortcutsProvider {
    static var appShortcuts: [AppShortcut] {
        AppShortcut(
            intent: AddTodoIntent(),
            phrases: ["\(.applicationName) Add a new todo"],
            shortTitle: "New Todo",
            systemImageName: "plus.circle"
        )
    }
}

systemImageName 是一个系统图标的名称,它将与快捷方式一起显示。

image.png


注意事项


1.快捷指令中 applicationName 必填,否则快捷方式将不被展示

image.png

image.png

2.快捷指令不完全属于小组件,经过试验发现代码文件放到主App依然有效

Siri与搜索

如需配置 Siri 调用可以进入快捷指令详情,点击【右上角按钮】开启 Siri

image.png

在搜索中输入项目名称便可以看到对应的快捷指令,执行状态会在灵动岛上展示

image.png

完整代码


import AppIntents

struct AddTodoIntent: AppIntent {
    static var title: LocalizedStringResource = "Add Todo"

    func perform() async throws -> some IntentResult & ProvidesDialog {
        // 实现添加待办事项的逻辑
        .result(dialog: "New todo added successfully.")
    }
}

struct ViewTodosIntent: AppIntent {
    static var title: LocalizedStringResource = "View Todos"

    func perform() async throws -> some IntentResult & ProvidesDialog {
        // 实现查看待办事项的逻辑
        .result(dialog: "Here are your todos...")
    }
}

struct TodoAppShortcuts: AppShortcutsProvider {
    static var appShortcuts: [AppShortcut] {
        AppShortcut(
            intent: AddTodoIntent(),
            phrases: ["\(.applicationName) Add a new todo"],
            shortTitle: "New Todo",
            systemImageName: "plus.circle"
        )

        AppShortcut(
            intent: ViewTodosIntent(),
            phrases: ["\(.applicationName) Show my todoso"],
            shortTitle: "Show todos",
            systemImageName: "plus.app"
        )
    }
}

本文同步自微信公众号 "程序员小溪" ,这里只是同步,想看及时消息请移步我的公众号,不定时更新我的学习经验。