Siri Shortcuts — how to implement them?

Piotr Smajek
Jul 26, 2019 · 5 min read

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. 😀

Image for post
Image for post

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? 🤔

Image for post
Image for post
Correct path to create Siri Shortcut
  1. 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.
  2. 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.
  3. 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? 👨‍💻

Image for post
Image for post

NSUserAcvitity — We will create a shortcut that will let us launch the screen for making a new order

  1. 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

Image for post
Image for post

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.

Image for post
Image for post
  1. Create an intent definition file. The best way is to add it to a separate framework in my project it will be in OrderKit.
Image for post
Image for post
File -> New -> File… -> SiriKit Intent Definition File

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.

Image for post
Image for post
Order definition — create custom intent
Order definition — Define responses

3. Create a new target with an extension for intentions, select “Include UI Extension” when you are creating it.

Image for post
Image for post
File -> New -> Target… -> Intents Extension
Image for post
Image for post
This is what the Target Membership should look like for Order.intentdefinition

4. Create a UI for intent in the MainInterface.storyboard file.

Image for post
Image for post
Creating UI for our Intent

5. Configure ready and success state in inIntentViewController.

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.

Image for post
Image for post

That’s all, everything works 🎉🎉🎉

You can check the whole project on my GitHub here:

Sources:

Miquido News

Publication on everything mobile, featuring future-forward…

Piotr Smajek

Written by

iOS Developer @miquido

Miquido News

Publication on everything mobile, featuring future-forward ideas on software development, product design, research and development and digital transformation of businesses.

Piotr Smajek

Written by

iOS Developer @miquido

Miquido News

Publication on everything mobile, featuring future-forward ideas on software development, product design, research and development and digital transformation of businesses.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store