Siri shortcuts, a new feature presented at last year’s WWDC, offer the ability to perform certain functionalities of the application in the background. We do not have to turn on the application to use these functionalities in the application, we can simply say to Siri, e.g. “Burger Time” and the system knows to which application this shortcut is assigned and orders a burger for us. 😀
A short description of Siri Shortcuts 📝
- A convenient way to accomplish tasks from the lock screen or search area.
- Shortcuts can also be added to Siri to run with a voice phrase on iOS, HomePod and watchOS.
- Developers add shortcut hooks into their apps that enable Siri to connect to these shortcuts.
- Using ML models of your behavior to analyze common functions that you perform and suggest shortcuts automatically.
- iOS 12+
In this article we will focus on the implementation of the order shortcut, one of the most popular applications of Siri Shortcuts. We will create a simple application that will allow you to order burgers 🍔
How can we create a shortcut? 🤔
- At the very beginning, we have to think about the functionality which the user really wants to make easier in our application. This should be a functionality that the user performs frequently and requires the user to enter the application and pass a long path in it. Shortcuts will enable shortening this path to perform the functionality.
- In the next step, you need to donate a shortcut. What does this mean? We must pass the information to the system about the fact that we have placed an order, for example one burger with bacon. We provide this information to the system and later Siri can suggest us at a specific time to make this specific order.
- The last thing to do is to handle this shortcut, which happens when the user tells Siri the command defined earlier, e.g. “Burger time”, or in the case when the user presses the shortcut suggested by Siri on the lock screen.
Ok, maybe we’ll write some code? What are our possibilities as developers? 👨💻
NSUserAcvitity — We will create a shortcut that will let us launch the screen for making a new order
- Add the key to Info.plist
2. Donate the shortcut when the user enters the screen
3. In the
AppDelegate, you must implement
continueUserActivity delegate which, after getting this particular type, will redirect to the given screen
Intents- We will implement a shortcut to make orders
Pro tip before implementation
Intentions appear when the user does something systematically. However, when you want to test this functionality so that you always get a suggestion from Siri, you must enable two options — “Display Recent Shortcuts” and “Display Donations on Lock Screen” in Settings -> Developer.
- Create an intent definition file. The best way is to add it to a separate framework in my project it will be in
2. Select a category, in this case “Order”. In this file we have to choose the parameters that we will pass to the system and create combinations of these parameters.
3. Create a new target with an extension for intentions, select “Include UI Extension” when you are creating it.
4. Create a UI for intent in the
success state in in
6. After creating the UI, we can go to the donate of our intention. In this case, it is best to donate intentions when placing the order. We provide all the information to the system that we previously defined in the file
Order.intentdefinition, i.e. burger name, quantity and additions, and the proposed phrase displayed in the case when we would like to immediately add this shortcut to Siri when placing the order.
7. Now we can handle the situation what is going to happen in the case when the user gets the suggestion of a shortcut and clicks on it and in case the user calls the shortcut by Siri.
That’s all, everything works 🎉🎉🎉
You can check the whole project on my GitHub here:
Sample app for Siri Shortcuts. Contribute to piotrsmajek/SiriShortcuts development by creating an account on GitHub.