Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
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.
Mastering Siri Shortcuts in iOS | Udemy
Getting Started with Siri Shortcuts in iOS 12 Using Intents was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.