Before you go, check out these stories!

0
Hackernoon logoGetting Started with Siri Shortcuts in iOS 12 Using Intents by@azamsharp

Getting Started with Siri Shortcuts in iOS 12 Using Intents

Author profile picture

@azamsharpMohammad Azam

https://cdn-images-1.medium.com/max/2000/1*bhonbY4jHhFNGXzBkodmbw.png

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.

What areย Intents?

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.

Defining yourย Intents:

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.

Donating 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:

Thatโ€™s it!

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.

Nice!

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.

Mastering Siri Shortcuts forย iOS

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.

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.