4

我需要将一个变量从 Intent Handler 传递给 IntentViewController 而不将其包含在响应中,以便它显示在自定义 UI 中,而无需 Siri 说出来。

到目前为止,我只能使用这样的响应来传递它们:

completion(GetTimesIntentResponse.success(name: "SomeName", time: "5:40", location: LatestLocation.City))

但是,这意味着 Siri 必须将其作为响应的一部分说出来。我想要的只是location显示在标签中(在自定义 UI 中),而无需 Siri 将其读出。

4

3 回答 3

4

INUIHostedViewControlling文档:

Intents UI 应用程序扩展与相应的 Intents 应用程序扩展协同工作。Intents 应用程序扩展处理意图并提供包含来自您的应用程序的数据的响应。Intents UI 应用扩展中的视图控制器以 INInteraction 对象的形式接收该数据,并使用它来配置其视图。

因此,在您的 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)
}
于 2019-02-21T00:17:54.203 回答
0

如果您想访问您的意图属性,您可以在 IntentViewController 中执行此操作:

func configureView(for parameters: Set<INParameter>, of interaction: INInteraction, interactiveBehavior: INUIInteractiveBehavior, context: INUIHostedViewContext, completion: @escaping (Bool, Set<INParameter>, CGSize) -> Void) {

    let intent = interaction.intent as! YourIntent
    // let foo = intent.yourProperty // access your property from your intent
}

我希望这就是你的意思。

于 2018-10-26T12:04:08.607 回答
-1

我没有设法找到直接执行此操作的方法,但是解决方法是将变量保存到 Intent Handler 中的 Core Data 并在 IntentViewController 中获取它们。这只是一种解决方法,但在我的情况下是有效的,因为我只需要传递一个变量,并且已经将它存储在应用程序其他地方的核心数据中。

于 2018-10-23T20:53:00.617 回答