【问题标题】:How to init INStartWorkoutIntent properly in Swift 3?如何在 Swift 3 中正确初始化 INStartWorkoutIntent?
【发布时间】:2017-04-01 17:02:28
【问题描述】:

我知道它有一个内置模板。

我转到“文件”菜单并选择“新建”>“目标”

从左侧窗格中选择 iOS > 应用程序扩展。

现在选择 Intents 扩展。

这将创建两个新组:YourExtension 和 YourExtensionUI。如果您打开 YourExtension 组,您将看到 IntentHandler.swift,其中包含一些用于处理锻炼的示例代码。

这里有一个更简单的示例来帮助您入门:

class IntentHandler: INExtension, INSendMessageIntentHandling {
    override func handler(for intent: INIntent) -> AnyObject {
        // This is the default implementation.  If you want different objects to handle different intents,
        // you can override this and return the handler you want for that particular intent.
        return self
    }

    func handle(sendMessage intent: INSendMessageIntent, completion: (INSendMessageIntentResponse) -> Void) {
        print("Send message: " + (intent.content ?? "No message"))

        let response = INSendMessageIntentResponse(code: .success, userActivity: nil)
        completion(response)
    }
}

我做到了,没关系。

现在我的问题是关于使用INStart​Workout​Intent 而不是 INSendMessageIntent,我应该怎么做?是否也有用于此意图的内置模板?

【问题讨论】:

    标签: ios swift siri


    【解决方案1】:

    最后,我自己解决了这个问题。

    当您想正确使用 INStartWorkoutIntent 时,您只需删除所有内置模板内容。

    您还必须将 INSendMessageIntentHandling 替换为 INStartWorkoutIntent 处理。

    public func handle(startWorkout intent: INStartWorkoutIntent, completion: @escaping (INStartWorkoutIntentResponse) -> Swift.Void) {
        let userActivity = NSUserActivity(activityType: NSStringFromClass(INStartWorkoutIntent.self))
        let response = INStartWorkoutIntentResponse(code: .continueInApp, userActivity: userActivity)
        completion(response)
    }
    

    不要忘记:

    对于您新创建的 Intents 目标,完全展开 NSExtension 字典以研究其内容。该字典更详细地描述了您的扩展支持哪些意图,以及您是否希望允许用户在设备锁定时调用意图。

    如果您想支持多个意图,请在顶部插入最相关的意图。 Siri 使用此顺序来确定用户想要使用哪一个,以防出现歧义。

    我们现在需要定义我们想要支持的意图。

    例如,我想构建一个支持支付意图的扩展。修改Info.plist文件,匹配下图。

    这里我们指定我们要处理 INSendPaymentIntent 并且我们需要解锁设备。我们不希望陌生人在设备丢失或被盗时发送付款!

    最后一件事就是在运行时设置目标 Intent 并完成。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 2010-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多