swift - Xcode Siri 意图 : How to pass a variable from Intent Handler to IntentUI ViewController without including it in the response

标签 swift xcode sirishortcuts

我需要将一个变量从 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/

相关文章:

xcode - 如何在 Xcode 中更改标签文本的颜色

xcode - 在模拟器中安装应用程序时,Simctl install 无法在 .app 中找到 Bundle Identifier

ios12 - Siri 快捷方式 : "Use with Siri" not showing in settings. 不允许使用 Siri

ios - Siri 快捷键 : How to require unlocking the phone

ios - 在 Firebase 中设置最大子级数

ios - 如何在模型中搜索数组属性

ios - 使用具有带 FlowLayout 的 UICollectionView 的单元格滚动 UITableView 时,应用程序崩溃

iphone - 我不知道如何在 iOS 6 中进行纵向和倒置

xcode - 从新的 Firebase 中检索数据