You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现Siri单句触发带参数的自定义AppIntent

如何实现Siri单句触发带参数的自定义AppIntent

嘿,我来帮你搞定这个Siri分两步触发的麻烦问题!你现在想直接用一句类似「Start my intent that does command xyz」的话,一次性触发带参数的自定义AppIntent对吧?其实核心就是得让Siri能读懂你整句话里的参数部分,咱们调整下代码的配置就能解决。

先理清楚问题根源:你的MyIntent虽然已经定义了context参数,但默认情况下Siri没把整句指令里的参数和Intent关联起来,咱们得给Intent和参数加上自然语言训练短语,直白点就是告诉Siri:“嘿,这种句式里的某部分就是我要的参数哦”。

下面是修改后的完整代码,我把关键改动都标出来了:

struct MyIntent: AppIntent {
    static var title = LocalizedStringResource("Start My Intent")
    static var openAppWhenRun: Bool = true
    static var description = IntentDescription(
        "Starts your custom intent with a specific command",
        category: .other,
        // 关键1:给Intent加几个带参数的完整指令示例,让系统学习自然语言逻辑
        examples: [
            IntentExample("Start my intent that does command xyz"),
            IntentExample("Run my intent with command open settings"),
            IntentExample("Trigger my intent to refresh data")
        ]
    )
    
    @Parameter(
        title: "My Intent Command",
        description: "The command you want the intent to perform",
        // 关键2:给参数配置支持的自然语言模板,让Siri知道哪里是参数值
        supportedUtterances: [
            "\(\.$0) that does command",
            "with command \(\.$0)",
            "to \(\.$0)"
        ]
    )
    var context: String
    
    @MainActor func perform() async throws -> some IntentResult & ReturnsValue<String> {
        UserDefaults.standard.set(context, forKey: "my.intent.command")
        AppBridge.processIntent(with: context)
        return .result(value: "\(context)")
    }
}

再给你拆解下这些改动的作用:

  • Intent的examples:相当于给Siri递了几份“标准答案”,告诉它用户可能会用哪些完整句子触发这个带参数的Intent,系统会根据这些示例训练识别逻辑,用多了类似的表述也能被识别。
  • Parameter的supportedUtterances:定义了参数在自然语言里的常见嵌入方式,\(\.$0)代表参数值的位置,比如模板"with command \(\.$0)",Siri就能自动抓取「Run my intent with command open settings」里的open settings作为context参数的值。

最后再提几个实用的注意事项:

  1. 修改完代码后,建议先卸载重装App,让系统重新把Intent的配置同步给Siri。
  2. 可以去「设置 → Siri与搜索 → 你的App」里确认Siri权限已经开启,确保Intent被系统正确索引。
  3. 刚开始测试的时候尽量用你在examples里写过的句式,等Siri熟悉你的指令逻辑后,稍微灵活点的表述也能被识别到。

这样调整后,你就能直接用单句指令触发带参数的Intent啦,不用再分两步操作~

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.07 12:08:06