代码之家  ›  专栏  ›  技术社区  ›  Razattax

Xcode Siri Intents:如何将一个变量从Intent处理程序传递到IntentUI ViewController,而不将其包含在响应中

  •  1
  • Razattax  · 技术社区  · 6 年前

    我需要将一个变量从Intent处理程序传递到IntentViewController,而不将其包含在响应中,这样就可以在自定义UI中显示它,而无需Siri说出来。

    到目前为止,我只能用这样的回答通过它们:

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

    然而,这意味着Siri必须将此作为回应的一部分。我想要的只是 location 在标签中显示(在自定义UI中),而无需Siri读取。

    3 回复  |  直到 6 年前
        1
  •  4
  •   SlimeBaron    6 年前

    INUIHostedViewControlling 文档:

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

    所以,在你的意图中:

    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)
    }
    

    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)
    }
    
        2
  •  0
  •   Matt    6 年前

    如果要访问intent属性,可以在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
    }
    

    我希望这就是你的意思。

        3
  •  -1
  •   Razattax    6 年前

    我没有设法找到一种方法来直接这样做,但是解决方法是将变量保存到Intent处理程序中的核心数据,然后在IntentViewController中获取它们。这只是一个解决方法,但在我的情况下是有效的,因为我只需要传递一个变量,并且已经将它存储在应用程序的其他核心数据中。