如何基于App Intents实现与Siri的连续对话?
实现Siri连续对话的可行方案
1. 启用App Intents会话延续
在perform()方法返回的IntentResult中,通过shouldContinueSession参数让Siri保持监听状态,同时在回复话术里加入引导提示,用户就能直接继续输入,无需重复触发"嘿Siri"。
修改你的perform()方法:
func perform() async throws -> some IntentResult { guard let providedPhrase = phrase else { throw $phrase.needsValueError("What do you need help with?") } let request = Request() let reply = request.sendRequest(transcription: providedPhrase) // 保持会话并给出引导,让用户知道可以继续提问 return .result( dialog: "\(reply)\nWhat else can I assist you with?", shouldContinueSession: true ) }
2. 配置后续关联意图
如果需要更结构化的会话流程,可以定义关联的后续意图,通过suggestedIntents参数推荐给Siri,让系统自动识别用户的后续请求并匹配对应逻辑。
示例后续意图:
struct SiriFollowUpLuna: AppIntent { static var title: LocalizedStringResource = "Ask Luna Again" static var description = IntentDescription("Continue asking Luna questions.") @Parameter(title: "Next Question") var nextPhrase: String func perform() async throws -> some IntentResult { let request = Request() let reply = request.sendRequest(transcription: nextPhrase) return .result( dialog: "\(reply)\nNeed further help?", shouldContinueSession: true, suggestedIntents: [SiriFollowUpLuna()] ) } }
然后在原意图的perform()中加入推荐:
return .result( dialog: "\(reply)\nWhat else can I assist you with?", shouldContinueSession: true, suggestedIntents: [SiriFollowUpLuna()] )
3. 应用内持续语音交互(进阶方案)
如果需要完全在应用内控制对话流程,可以结合INUIVoiceCommandCenter或自定义语音输入组件,配合Siri授权,实现应用内的持续语音交互,无需依赖系统Siri的会话限制。
注意事项
- Siri会话有超时限制,长时间无输入会自动结束会话,建议在回复中明确引导用户继续操作
- 所有Siri交互功能需在真实iOS设备上测试,模拟器的Siri支持有限
- 确保应用已正确配置Siri权限,在
Info.plist中添加NSSiriUsageDescription字段
内容的提问来源于stack exchange,提问作者Matteo Barberis
相关产品推荐
相关产品推荐

