Siri 发起快捷指令

217 阅读2分钟

1.创建快捷指令

在主工程中创建实现协议AppIntent 的结构体

struct OpenSpecificPageIntent: AppIntent {
    // 这个 title 属性, 就是在快捷指令 App 中显示的指令名称
    static var title: LocalizedStringResource = "小智开关"
    // 是否在快捷指令、spolight、系统搜索服务等被发现, 默认是 true
    static var isDiscoverable: Bool = true
    // 对指令的详细描述 (可选)
    static var description: IntentDescription? = "对指令的详细描述"
    
    func perform() async throws -> some IntentResult {
        print("执行具体的操作")
        return .result()
    }
}

上面的快捷指令, 不会拉起app

1.1 是否拉起app
// 是否拉起主 app
static var openAppWhenRun: Bool = true
1.2 执行结果返回一个值

值的类型是由具体的规定的, 一般只支持基础数据类型(如 String, Int, Float, double, bool) 等

func perform() async throws -> some IntentResult & ReturnsValue<String> {    
    return .result(value: "hello")
}

注意 ReturnsValue 的配置

1.3 弹出默认的 dialog
func perform() async throws -> some IntentResult & ProvidesDialog {
   return .result(dialog:"这是默认的dialog")
}

注意ProvidesDialog 的配置

1.4 自定义 Dialog 弹窗内容
// @MainActor 在主线程执行
@MainActor 
func perform() async throws -> some ReturnsValue<String> & ProvidesDialog & ShowsSnippetView {
    return .result(
            value: "成功",
            dialog: "CustomViewAppIntent",
            view: CustomView())
    }
    
@ViewBuilder
private func CustomView() -> some View {
     VStack(spacing: 30) {
            Image(systemName: "face.smiling.inverse")
            Text("Finished CustomViewAppIntent")
            .padding()
      }
  }       

1.5 AppIntent 支持进行链式的调用

func perform() async throws -> some OpensIntent {
        return .result(opensIntent: TeaIntent())
}

struct TeaIntent: AppIntent {
    static var title = LocalizedStringResource("无用户输入")
    static var description = IntentDescription("无用户输入的Intent")

    @MainActor
    func perform() async throws -> some IntentResult {
        print("链式处理逻辑部分")
        return .result()
    }
}

注意 OpensIntent 的声明

2.注册快捷指令

2.1 在快捷指令app 中自动添加
struct TodoAppShortcuts: AppShortcutsProvider {
    
    @AppShortcutsBuilder static var appShortcuts: [AppShortcut] {
        AppShortcut(
            intent: OpenSpecificPageIntent(),
            phrases: ["(.applicationName)开关"],
            shortTitle: "开关",
            systemImageName: "power.circle"
        )
        AppShortcut(
            intent: QRCodeIntent(),
            phrases: [
                "打开(.applicationName)二维码",
                "(.applicationName)二维码",
                "(.applicationName)码",
                "(.applicationName)维码",
                "(.applicationName)二码",
                "(.applicationName)二",
            ],
            shortTitle: "二维码",
            systemImageName: "qrcode.viewfinder"
        )
    }
}

其中 shortTitle 为指令名称、systemImageName 为系统图标

这样就可以在快捷指令 app 中可以看到了:

2.2 快捷指令的执行方式
  • 通过 siri 调用 phrases 中指定的名称唤起;

  • 通过快捷指令 App 中的按钮icon 手动点击执行;

  • 通过系统搜索服务, 搜索应用名称, 就可以匹配到相关指令, 手动点击执行;

2.3 Siri 语音发起快捷指令

配置 phrases

phrases: [ "打开(.applicationName)二维码", "(.applicationName)二维码", "(.applicationName)码", "(.applicationName)维码", "(.applicationName)二码", "(.applicationName)二", ],

必须指定 applicationName, 不然无法识别, 这个关键词要尽可能多, 不然触发概率很低。