If you read our WWDC 2018 Highlights post, you may remember that Siri Shortcuts was one of the announcements that we were most excited about here at We Are Mobile First HQ.
As an early adopter in the days of Voice Control, I still remember the confused looks I would get from people around me as I spoke into the handset and held a conversation with Siri. Siri’s performance, however, has continually fallen short of its potential. It isn’t just me who feels that way either. When I spoke with some of my colleagues about an Apple Machine Learning post that claimed Siri was getting better, some of them expressed the view that Siri was actually getting worse.
If an AI assistant has a lower probability of properly handling a request than you just typing or tapping something yourself, you’re going to stop using it and that’s exactly what I did. I couldn’t help but lament the lack of opportunities when it came to accessing apps through Siri, as it was clear to me how it could accelerate the process of giving me directions or playing a specific song or even booking a hotel room. The potential was there. The development, however, was not.
With the announcement of iOS 12, it seems Apple is making tentative steps in this direction with the introduction of Siri Shortcuts. What are Siri Shortcuts? According to Apple, Shortcuts can ‘intelligently pair users’ daily routines with your apps…to help users quickly accomplish tasks related to your app, directly from the lock screen, in Search, or from the Siri watch face’. They essentially allow you to expose the capabilities of your app to Siri.
This is what Apple says every Shortcut should be able to do:
Available from iOS 12 onwards, there’s now a ‘Shortcuts’ app (yes, it is the renovated Workflow app) so anyone can build their own just by dragging and dropping a series of steps together. That’s something we’ll leave for the non-developers to discover themselves.
What we are interested in showcasing here, however, is how we can expose our apps capabilities to SIri. You need to make it clear to Siri what shortcuts your app is capable of – exposing and defining each before donating it to the system. The system will then, hopefully, recognise when the user has done something in-app that you’ve exposed a shortcut for.
Firstly, there are two APIs that support Shortcuts. The first is NSUserActivity.
NSUserActivity should be used if your shortcut:
<p> CODE: "https://gist.github.com/WildStudio/d1edc9fd365381c737d5a1b6568a07c2.js"</p>
The second API is intents.
A couple of years ago, Apple introduced the ability to add intents to your apps using SiriKit. This year’s WWDC Siri Shortcuts announcement adds the ability to define custom intents.
How to build an intent:
Let’s go through the new Intent Editor in Xcode. Start by adding a New File and choose the SiriKit Intent Definition File type.
You then need to click the plus button on the bottom left to get started.
Firstly, we want to name our intent before filling out its metadata. The first piece of metadata is ‘Category’. Defining the category lets Siri know how to talk about the intent and, thus, how to display it in the UI.
In this example, we wanted to be able to sell flight tickets from a departure location to an arrival location on a specific date. We, thus, chose the ‘Buy’ category. We then added a title and a description before checking the ‘user confirmation’ checkbox in order to prevent Siri from buying tickets without confirmation. We then added the parameters item and delivery location.
Parameters support a short list of types, including string, number, person and location. If you are building a parameter that represents an object that’s in your app, you can choose a ‘Custom’ type. Make sure you define both the shortcut and the type of shortcut to be suggested to the user in the ‘Shortcut types’ section. Each type includes a specific combination of parameters. In the case of our flight ticket app, we wanted to support background execution once the arrival and destination locations had been predicted. Background execution allows the user to complete an action without opening the app, affording them a much better user experience. If your shortcut can’t be performed in the background, make sure you uncheck the ‘Supports background execution’ box.
Make sure you take care and provide the types for every variant of your shortcut that you think may be useful in order to help Siri make the best predictions. You can provide up to 16 shortcut types for every intent you define. Once you’ve defined your intent, Xcode will generate an intent class and an intent handling protocol. Your intent definition should be included in every target in which your intent is used. Be sure to double check that you haven’t duplicated instances of your intent class.
And that is how you create a shortcut for Siri Shortcuts!
In a future post, I’ll look at how you can then donate and handle Shortcuts. Until then, why not catch up on some of our existing blog posts? From time management for developers to realising the full power of source control, there’s plenty to keep you busy…
(Image credit: Apple)