我需要将一个变量从 Intent Handler 传递到 IntentViewController 而无需将其包含在 Response 中,以便它显示在自定义 UI 中而无需 Siri 说出来。
到目前为止,我只能使用这样的响应传递它们:
completion(GetTimesIntentResponse.success(name: "SomeName", time: "5:40", location: LatestLocation.City))
然而,这意味着 Siri 必须将此作为响应的一部分。我只想让 location
显示在标签中(在自定义 UI 中),而无需 Siri 读出它。
最佳答案
来自 INUIHostedViewControlling
文档:
An Intents UI app extension works in tandem with a corresponding Intents app extension. The Intents app extension handles the intent and provides a response that includes data from your app. The view controller in your Intents UI app extension receives that data in the form of an INInteraction object and uses it to configure its view.
因此,在您的 IntentHandler 中:
func handle(intent: GetTimesIntent, completion: @escaping (GetTimesIntentResponse) -> Void) {
let activity = NSUserActivity(activityType: "com.your_company.your_identifier.get_times")
activity.userInfo?["times_time"] = "5:40"
activity.userInfo?["times_location"] = LatestLocation.City
let response = GetTimesIntentResponse(code: .success, userActivity: activity)
completion(response)
}
在您的 IntentViewController 中:
func configureView(for parameters: Set<INParameter>, of interaction: INInteraction, interactiveBehavior: INUIInteractiveBehavior, context: INUIHostedViewContext, completion: @escaping (Bool, Set<INParameter>, CGSize) -> Void) {
let data = interaction.intentResponse?.userActivity?.userInfo
let time = data?["times_time"] as? String ?? "No time"
let location = (data["times_location"] as? LatestLocation.City)?.name ?? "No city"
label.text = "\(location) at \(time)"
completion(true, parameters, self.desiredSize)
}
关于swift - Xcode Siri 意图 : How to pass a variable from Intent Handler to IntentUI ViewController without including it in the response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52749530/