Siri无法识别App Intents短语参数的问题排查
问题
我使用App Intents框架开发iOS应用,已创建OpenShowIntent支持两种外部打开节目的方式:
- 调用快捷指令并提供节目名称;
- 对Siri说出指令:
Open [show name] in [app name]
通过快捷指令App手动调用时功能正常,但使用Siri语音指令时,Siri能识别应用名称,却无法捕获节目名称,执行后返回“Something went wrong”。需要解决该问题,让用户说出“Open {showname} in {appName}”时能在应用中打开对应节目。
原因分析
- 参数歧义:同时定义了
ShowEntity类型的可选参数show和字符串类型的showName参数,Siri处理语音指令时无法正确映射节目名称到对应参数,且逻辑优先使用show参数,导致解析冲突。 - 参数绑定错误:参数摘要和快捷短语都绑定到了
showName字符串参数,但Siri对实体类型参数的语音识别支持更完善,字符串参数无法有效提取语音中的节目名称。
解决方案
- 移除冗余的
showName参数,仅保留ShowEntity类型参数作为唯一入口,让Siri通过实体查询自动处理节目名称的识别与匹配。 - 调整参数摘要和快捷短语,关联到
show实体参数。 - 优化Intent执行逻辑,直接基于实体参数处理业务流程。
修改后的代码示例
OpenShowIntent.swift
import Foundation import AppIntents import Models @available(iOS 16.0, *) struct OpenShowIntent: AppIntent { static var title: LocalizedStringResource = "Open a Show" static var openAppWhenRun = true // 移除showName参数,将show设为可选(保留用户未指定时的选择逻辑) @Parameter(title: "Show", optionsProvider: ShowEntityQuery()) var show: ShowEntity? static var parameterSummary: some ParameterSummary { // 关联到show实体参数 Summary("Open \(\.$show) in (APP NAME)") } @MainActor func perform() async throws -> some IntentResult & ProvidesDialog { let showToOpen: ShowEntity if let show = show { showToOpen = show } else { // 若未提供实体,调用查询获取推荐节目让用户选择 let suggestedEntities = try await ShowEntityQuery().suggestedEntities() showToOpen = try await $show.requestDisambiguation( among: suggestedEntities, dialog: "Choose a show from your bookmarks?" ) } let deepLink = DeepLink( type: .show( showId: showToOpen.id, showDateStr: nil, voucher: nil, reviewModalParams: nil ) ) let url = try deepLink.createURL(source: nil) IntentsHelper.openLink(url: url) return .result(dialog: "Show '\(showToOpen.name)' opened successfully.") } }
TTShortcuts.swift
import Foundation import AppIntents @available(iOS 16.0, *) struct TTShortcuts: AppShortcutsProvider { static var appShortcuts: [AppShortcut] { return [ AppShortcut( intent: OpenShowIntent(), phrases: [ "View show in \(.applicationName)", "Open \(\.$show) in \(.applicationName)", "Show \(\.$show) in \(.applicationName)", "Find \(\.$show) in \(.applicationName)", "Search for \(\.$show) in \(.applicationName)" ], shortTitle: "Open show", systemImageName: "pencil.circle" ) ] } static var shortcutTileColor: ShortcutTileColor = .blue }
额外说明
- 移除
showName后,Siri会自动将语音中的节目名称传递给ShowEntityQuery的entities(matching:)方法,获取匹配实体,无需手动处理字符串搜索。 - 若需要支持模糊搜索,
ShowEntityQuery已实现对应逻辑,Siri会返回匹配的节目列表,必要时请求用户确认。 - 测试时确保设备Siri语言与应用本地化设置一致,避免识别偏差。
内容的提问来源于stack exchange,提问作者user26425844
相关产品推荐
相关产品推荐

