In my last post I talked about how you can quickly create a Siri Shortcut using NSUserActivity. NSUserActivity is great for scenarios where you want to open your app through Shortcuts. In more advanced scenario, where you want finer control over the Shortcuts experience it is recommended that you use Intents.
In this post I will show you how you can get started with Intents and how you can setup intents definition file which serves as the blueprint for your Siri Shortcuts.
Intents allows you to have finer control over Shortcuts. This means instead of Siri saying
“Your coffee has been ordered and it will arrive at a location”
Siri can say something more meaningful like
“Your cappuccino has been ordered and it will arrive at your office in 10 minutes”
Intents can also be used to create custom responses and even a custom user interface which will allow users to place their order without even opening the app. Don’t worry too much about custom user interface for intents for now, I will cover that in the next post.
Intents are defined in an Intents definition file. Add a new file to your project and search for “intents” in the search bar. Select the “SiriKit Intent Definition File” as shown below:
By default the Intents file is empty. Click on the “+” sign in the bottom which adds a new intent. Rename the intent to “OrderCoffee” from its default name.
Intents definition file is divided into multiple sections. Let’s start with “Custom Intent” section at the top. This is where you define the name of your intent, description, category and the default image.
Since, our app is responsible for ordering coffee that is why we selected the category as “Order”. The Confirmation checkmark is checked indicating that user confirmation is required in order to execute this intent.
Next, we will setup the parameters for your intent. Parameters is the additional data you can pass to the intents. Parameters are also used by Siri when used with Siri Voice Shortcuts.
We are using two parameters coffeeName and deliveryLocation. One important thing to notice is that the deliveryLocation is of type Location and not String. This means we can even use it to calculate the estimated time of arrival.
You can also use your custom classes as Parameters. In those cases the Parameter type will be custom.
Finally, we will set “Shortcut Types”. Shortcut Types allows Siri to learn more about your Shortcut behaviors. Shortcut Types really depends on the app and should be defined in a way which makes sense. In our app we are defining two shortcut types so Siri can learn the behavior when coffee and location is available and also when the location is not available.
Go ahead and build your application. Behind the scenes Xcode will look at the Intents file and create a corresponding Swift class “OrderCoffeeIntent”. In the next section we will learn how to donate our intents.
Finally, the time has come to donate our intents. We start by creating a placemark instance which represents, where the coffee needs to be delivered. This can be done by using the CLLocation and CLPlacemark classes as shown below:
Next, we will create an instance of OrderCoffeeIntent class, which was automatically generated by the Intents Definition File.
Finally, we will donate our intent using the INInteraction instance as shown below:
Next run the app and donate your intent. After you have donated the shortcut you should be able to see your donations under “Siri App Suggestions” as shown below.
In the next post we will take our app to the next level by implementing a custom user interface for your shortcut. This will allow us to place an order for coffee without even opening the app.
If you have enjoyed this post and want to learn more about Siri Shortcuts then check out my 3+ hour Udemy course “Mastering Siri Shortcuts in iOS”. The course goes into much more detail in how to create Siri Shortcuts using NSUserActivity, Intents, Custom Responses, Custom User Interface and much more.