Swift 3 - How to init INStart​Workout​Intent properly?

不羁的心 提交于 2020-02-28 06:33:28

问题


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.

Now choose Intents extension.

That will create two new groups: YourExtension and YourExtensionUI. If you open the YoureExtension group you'll see IntentHandler.swift, which contains some sample code for handling workouts.

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 ? Is there a built-in template for this intents too ?


回答1:


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.

You have also to replace INSendMessageIntentHandling by INStartWorkoutIntent Handling.

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. 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.

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.

Here we specify that we want to handle the INSendPaymentIntent and that we require the device to be unlocked. 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.



来源:https://stackoverflow.com/questions/43159662/swift-3-how-to-init-instartworkoutintent-properly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!