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

严格上说快捷指令并不属于小组件,只是和小组件一样通过  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"
        )
    }
}

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

阅读全文
下载说明:
1、本站所有资源均从互联网上收集整理而来,仅供学习交流之用,因此不包含技术服务请大家谅解!
2、本站不提供任何实质性的付费和支付资源,所有需要积分下载的资源均为网站运营赞助费用或者线下劳务费用!
3、本站所有资源仅用于学习及研究使用,您必须在下载后的24小时内删除所下载资源,切勿用于商业用途,否则由此引发的法律纠纷及连带责任本站和发布者概不承担!
4、本站站内提供的所有可下载资源,本站保证未做任何负面改动(不包含修复bug和完善功能等正面优化或二次开发),但本站不保证资源的准确性、安全性和完整性,用户下载后自行斟酌,我们以交流学习为目的,并不是所有的源码都100%无错或无bug!如有链接无法下载、失效或广告,请联系客服处理!
5、本站资源除标明原创外均来自网络整理,版权归原作者或本站特约原创作者所有,如侵犯到您的合法权益,请立即告知本站,本站将及时予与删除并致以最深的歉意!
6、如果您也有好的资源或教程,您可以投稿发布,成功分享后有站币奖励和额外收入!
7、如果您喜欢该资源,请支持官方正版资源,以得到更好的正版服务!
8、请您认真阅读上述内容,注册本站用户或下载本站资源即您同意上述内容!
原文链接:https://www.shuli.cc/?p=21419,转载请注明出处。
0

评论0

显示验证码
没有账号?注册  忘记密码?