Sirikit开发咨询:Siri自定义文本反馈及航空App里程查询实现
嘿,这个需求完全可以搞定的,而且实现起来也不复杂。你只需要在handle(intent:completion:)方法里正确构建并返回INSearchForAccountsIntentResponse对象,就能让Siri在屏幕上显示你想要的文本回复。
下面是具体的实现步骤和代码示例:
第一步:获取账户和里程数据
先从传入的INSearchForAccountsIntent里拿到用户要查询的账户信息(比如账户号),或者从你的App数据源里获取对应的里程数。第二步:构建自定义回复文本
按照你想要的格式拼接好回复内容,比如"Your miles for the account no: 1000211 is 3000"。第三步:创建并配置响应对象
创建一个状态为.success的INSearchForAccountsIntentResponse,然后把你的自定义文本设置给它的spokenPhrase属性——这个属性既控制Siri的语音播报内容,也会在屏幕上显示出来。如果需要在通知推送里也显示这段内容,还可以设置localizedUserNotificationMessage属性。第四步(可选):返回账户详情对象
如果你想让Siri更清晰地展示账户信息(比如把里程数作为账户详情的一部分),可以创建INAccount和INAccountDetails对象,添加到响应的accounts数组里。
完整代码示例
func handle(intent: INSearchForAccountsIntent, completion: @escaping (INSearchForAccountsIntentResponse) -> Void) { // 1. 获取目标账户号(这里示例用固定值,实际从intent或你的数据源获取) let targetAccountNumber = intent.account?.identifier ?? "1000211" // 2. 获取对应的里程数(实际从你的App后台或本地数据获取) let userMiles = 3000 // 3. 构建你想要的回复文本 let responseText = "Your miles for the account no: \(targetAccountNumber) is \(userMiles)" // 4. 创建成功的响应对象 let response = INSearchForAccountsIntentResponse(code: .success, userActivity: nil) // 5. 设置语音播报和屏幕显示的文本 response.spokenPhrase = responseText // 可选:设置通知里显示的文本 response.localizedUserNotificationMessage = responseText // 可选:构建账户详情对象,让Siri展示更丰富的信息 let airlineAccount = INAccount(identifier: targetAccountNumber, name: "Your Airline Account", type: .other) let accountDetails = INAccountDetails( account: airlineAccount, nickname: nil, balance: nil, secondaryBalance: nil, details: [INAccountDetail(key: "Miles", value: "\(userMiles)", type: .other)] ) response.accounts = [accountDetails] // 6. 返回响应给Siri completion(response) }
这样实现后,当用户通过Siri查询账户里程时,Siri就会在屏幕上显示你自定义的文本,同时语音播报这段内容啦。
内容的提问来源于stack exchange,提问作者lifemoveson

