SiriKit Custom Intents parameters and shortcuts

4.2k views Asked by At

I'm trying to figure out how to define and create shortcuts using the Custom Intents in iOS12. I want to create a shortcut so that a user can add a task to my app and give it a name (and other parameters in the future). For e.g. just say to Siri "Add Task with name 'Pick up laundry'".

Here is how I've defined the intent: enter image description here

Then I am trying to create a Siri shortcut using this:

    TaskIntent *taskIntent = [[TaskIntent alloc] init];
    taskIntent.suggestedInvocationPhrase = @"TaskIt";

    INShortcut *taskShortcut = [[INShortcut alloc] initWithIntent:taskIntent];

    INUIAddVoiceShortcutViewController *addSiri = [[INUIAddVoiceShortcutViewController alloc] initWithShortcut:taskShortcut];
    addSiri.delegate = self;
    [self presentViewController:addSiri animated:YES completion: nil];

However, it ends up with an exception in the app:

Cannot create shortcut from intent ( { taskName = ; identifier = EE4661C6-1826-4A14-84EC-FD684647FE2B; }) because it has no valid parameter combinations

So I'm not sure how to do this correctly. Do I have to give a taskName when donating the shortcut? But that will mean the same taskName is shown and added to the system each time, which is not what I want. Or is the new SiriKit not customizable enough to handle generic input?

2

There are 2 answers

2
HongchaoZhang On

You cannot ask Siri questions with parameters for custom intent you created.

There are two types of intents you can create in SiriKit:

  • Built-in intent
  • Custom intent

For built-in intent, you can ask Siri questions with parameters, like "Send John a message saying 'Help to book me a ticket'". SiriKit will help to find the intent, extract the parameters (John, and the message content), and send the message for you.

For custom intent, which is for Siri shortcut, you cannot ask questions with parameters. You can only use a command without parameters to trigger the intent, and do the same thing as it was donated (When you donate the custom intent, you have to set the parameters in it.).

The difference between built-in intent and custom intent may be caused by the following fact: For a built-in intent, a lot of machine learning training work is needed for SiriKit to recognize the intent and extract the parameters. But the custom intent has no such trained machine learning model, and it cannot act like build-in intents.

0
KerstinCoder On

Yes, after having created your custom TaskIntent, you need to populate its parameters. That is, you need to set a value for taskName:

taskIntent.taskName = @"Test";

You donate the intent every time the user does the related action in your app. If your user enters a task with a taskName, you can donate this taskName by creating your intent and setting the taskIntent.taskName parameter to the task name your user put in. It will suggest a shortcut for entering a task with the same title again, which is probably not what you want. Adding tasks is not repetitive due to different task titles and nothing which can be predicted, so the INAddTasksIntent seems to be a better fit than donating shortcuts.