How to Integrate Siri Shortcuts and Design Custom Intents [Tutorial]

Ami Intwala
Simform Engineering
8 min readOct 14, 2021
Photo credits: Google
Photo credits: Google

Introduction to Siri

Siri makes it easy for people to accomplish everyday tasks quickly using voice, touch, or automation. When you use SiriKit to define the tasks and actions that your app supports, people can use Siri to perform them even when your app isn’t running.

If your app lets people perform an everyday task that doesn’t fit into any of the SiriKit domains, you can create a custom intent to represent it. You can also use a custom intent to support a shortcut, which gives user a way to initiate frequently performed actions by speaking a simple phrase.

Siri Shortcuts

The release of Siri Shortcuts gave more opportunities to both app developers and iOS users. This useful feature groups iOS actions that can be triggered with custom voice commands.

First of all iOS 13 has an issue where Siri just redirects you to the shortcut in the shortcut’s app, rather than asking for input parameters. In iOS 13.1, Apple has given SiriKit more privileges to ask the user for input parameters in custom intent with Siri dialog.

Photo credits: Google

What we will build

We will be having two parts in this article, part one is to understand how to setup and add Siri Shortcut in the app. In second part, we will understand how to use custom intent UI.

I would like to provide a simple example on how we can use Siri Shortcut in Part 1. Also, we will build a simple Invoice Tracker App that will take user input (invoice title and price) from Siri, then display the data using custom Intent UI Extension as explained in Part 2.

Getting Started

GIF credits: GIPHY

Part 1 - Add Siri Shortcut

How to enable Siri in your project

To enable Siri in your project, please follow below steps.

  1. Select your project name in the project navigator
  2. Select the target of your project.
  3. Click on the Signing & Capabilities option
  4. Then click on the + capability button
  5. Add Siri.
How to enable Siri in your project

How to add Siri Intents and Intents UI

We will add two new targets as we need a custom intent that we can run outside the app, please follow below steps,

  1. Select your project in the project navigator.
  2. Select target.
  3. Add new target from the + button. You will get one window to choose a template for new target.
  4. Select Intent Extension.
  5. Click Next button.
Steps to add Intent Extension to your project

Please follow below steps to give a name to your intent extension and other configuration,

  1. Add product name which will be your intent extension name.
  2. Select “Include UI Extension” so that it will automatically add IntentUI.
  3. Click on Finish button.
Further steps to finish-up adding extension

The dialogs will come up for target scheme activation, please click “Activate”.

Target scheme activation dialog

Intents Definition with SiriKit Intent Definition File

Choose File > New > File > SiriKit Intent Definition File > Next

How to add SiriKit intent definition File
Search for SiriKit intent definition File

To add a new Intent, click on + button and give it proper name.

Add new Intent to how to add SiriKit intent definition File

Choose the category that describe your action and after that add the description. Now most important part is,

  1. We will add input parameters that Siri is going to ask users.
  2. Siri will ask all the parameters in its defined sequence. Considering our example, Siri will first ask for the title of the invoice and then it will ask for price.
  3. Siri will ask user for the title and price for our Invoice Tracker App.
Adding category and parameters in the SiriKit Definition File

You can see the further configuration for all the parameters in the below screenshot. You can also see the preview of summary for the Shortcut app.

Configuring summary for Shortcut App

Choose each input parameter and name it correctly, then add a message in the Siri dialog section, which Siri will speak while asking for input.

For our example, the response will have the total number of invoices created.

Handling response properties and templates for success

In the custom intents navigator, we configure how Siri will respond when our intent is executed.

Handling response template for failure case

Handling Custom Intent

In the project navigator, you will find the Intent Extension and Intent UI that we created earlier. In the intent extension, you will find the IntentHandler.swift file that Xcode created for us. This file handles all the intents. By overriding this function, we will provide class other than the extension’s class to handle a given intent. Please replace the code with below,

Now we need to create new intent handler file BillTrackerHandler.swift and write below method. Here, we have added one handling method which calculate the total numbers of invoices and called .success() with the intent value. See below code,

Now, we will add two more methods in BillTrackerHandler.swift file which will be parameters resolving methods, see below code for the same,

We must resolve all parameters successfully, in our case we want the user to enter all the input parameters so we used .needValue() which will ask the user for input value to resolve until we can call .success() with the intent value.

The last function that gets a previously saved list from UserDefaults if it exists, adds to it the new invoice item, updates the list in UserDefaults and returns its size for us to set as the response property that we predefined in the intent definition file.

Donating Shortcuts

Everyone is talking about donating shortcuts and intents. What is donating? If this sounds slightly confusing then allow me to explain. You can assign a shortcut which itself has an action associated with it that iOS will exposure to end user through places like Spotlight Search and Siri Suggestions based on user’s activity.

GIF credits: GIPHY

Donating the shortcuts means providing Siri with the information it needs to make appropriate suggestions regarding the actions that the user recently completed.

Write a below block of code to donate custom intent. Here, we have created an instance of INInteraction and donated it with the default values of intent parameters which is suggestedInvoicationPhrase. Let’s call it inside scene function in SceneDelegate.swift, don’t forget to import Intents at the top.

Now, we will be able to create a shortcut from the Shortcut App already installed in iPhone.

To add a shortcut in the Shortcut App, please follow the below steps,

  1. Go to the Shortcut App.
  2. Click on + button on top right corner. It will open a new window.
  3. Click on Add Action Button from that window and choose Apps.
  4. After choose the Apps, list of all the app which are installed and support Siri shortcut will be appear. Please choose your app from the list.
  5. Click on Next button.
  6. Please enter shortcut name. You just need to say “Hey Siri, your shortcut name” to run this shortcut. For example, we have enter shortcut name “Add Invoice” so you need to say “Hey Siri, add invoice”
  7. Click on Done button and you are all set.

After completing the above implementation, create and test the shortcut.

How to add shortcut

Part 2 - Configure Custom UI using IntentUI

To increase your app’s impact on the user, your app can opt to provide a custom UI for Siri. This optional custom UI can supplement Siri’s UI or completely replace it. Providing a custom UI can make your app’s Siri integration stand out, and it’s done by creating an Intents UI Extension.

We have already created an Intent UI Extension while adding the intent extension initially. So now, the BillTrackerIntentUI project is responsible for the interface of the shortcut.

Photo credits: Google

Setting up the UI

BillTrackerIntentUI bundle has MainInterface.storyboard, IntentViewController.swift file and info.plist file.

  1. Now, open up MainInterface.storyboard where you can see intent view controller.
  2. Add tableview controller in the view controller and set delegate and datasource. Create an outlet of tableview controller.
  3. Add tableview cell, give cell identifier and set cell style as Right Detail.
  4. Add the code in viewDidLoad method shown below.
  5. Please configure tableview delegate and datasource methods, add the code shown below.
Add tableview in view controller

In the below code, we have configured UITableView datasource methods and assigned values to cell title label and detail text label accordingly.

You can also set desired view controller size, please follow below code,

There is one more thing you need to do to make your custom UI appear. Open the info.plist file in BillTrackerIntentsUI project and add a reference to the BillTrackerIntent class as shown below:

Adding a value to info.plist

That’s it! Let’s run our application and add invoice via Siri :)

Conclusion

So, we’ve built our first fully app independent Siri Shortcut! Nice!
SiriKit is a very robust framework. You can ask for input from the user through Siri voice command and have them interact with your application in a thoroughly advance way.

You can find the above tutorial here.

I hope this article inspires you to build more amazing iOS apps using SiriKit. If you like this article then generous, genuine and generative feedback is appreciated. Don’t forget to throw a clap.👏

--

--