简体   繁体   English

如何在 Swift 3 中正确初始化 INStartWorkoutIntent?

[英]How to init INStartWorkoutIntent properly in Swift 3?

I know that there's a built-in template for it.我知道它有一个内置模板。

I go to the File menu and choose New > Target我转到“文件”菜单并选择“新建”>“目标”

Select iOS > Application extensions from the left-hand pane.从左侧窗格中选择 iOS > 应用程序扩展。

Now choose Intents extension.现在选择 Intents 扩展。

That will create two new groups: YourExtension and YourExtensionUI.这将创建两个新组:YourExtension 和 YourExtensionUI。 If you open the YourExtension group you'll see IntentHandler.swift, which contains some sample code for handling workouts.如果您打开 YourExtension 组,您将看到 IntentHandler.swift,其中包含一些用于处理锻炼的示例代码。

Here's a much simpler example to get you started:这是一个更简单的示例,可帮助您入门:

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

I did that, it's OK.我这样做了,没问题。

Now my issue is about using INStart​Workout​Intent instead of INSendMessageIntent, how am I supposed to?现在我的问题是关于使用INStart Workout Intent而不是 INSendMessageIntent,我该怎么做? Is there a built-in template for this intents too?是否也有针对此意图的内置模板?

Finally, I solved the question by myself.最后,我自己解决了这个问题。

When you want to use INStartWorkoutIntent properly, you have just to remove all the built-in template content.当您想正确使用 INStartWorkoutIntent 时,您只需删除所有内置模板内容。

You have also to replace INSendMessageIntentHandling by INStartWorkoutIntent Handling.您还必须将 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)
}

DO NOT FORGET:不要忘记:

To your newly created Intents target, fully expand the NSExtension dictionary to study its contents.对于新创建的 Intents 目标,完全展开 NSExtension 字典以研究其内容。 The dictionary describes in more detail which intents your extension supports and if you want to allow the user to invoke an intent while the device is locked.该词典更详细地描述了您的扩展支持哪些意图,以及您是否希望允许用户在设备锁定时调用意图。

Insert the most relevant intents at the top if you want to support more than one.如果您想支持多个意图,请在顶部插入最相关的意图。 Siri uses this order to figure out which one the user wants to use in case of ambiguity. Siri 使用此顺序来确定用户在出现歧义时想要使用的顺序。

We now need to define which intents we want to support.我们现在需要定义我们想要支持的意图。

For example, I want to build an extension that supports the payment intent.例如,我想构建一个支持支付意图的扩展。 Modify the Info.plist file to match the following picture.修改 Info.plist 文件以匹配下图。

在此处输入图片说明

Here we specify that we want to handle the INSendPaymentIntent and that we require the device to be unlocked.在这里我们指定我们要处理 INSendPaymentIntent 并且我们需要解锁设备。 We don't want strangers to send payments when the device is lost or stolen!我们不希望陌生人在设备丢失或被盗时付款!

Last thing just to set in target your Intent at the running and it's done.最后一件事就是在运行时设置目标你的意图,它就完成了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM