iOS 12 has now officially been released and it comes with a whole host of new features, from Screen Time to Siri Shortcuts.
As you may recall, I recently shared a blog post all about how to create Siri Shortcuts and define custom intents. In this second part, I’m going to explore how we can then donate these intents before demonstrating how to handle the shortcuts from our demo app.
‘Donating’ an intent means providing Siri with the information it needs to make relevant suggestions regarding actions that the user has recently completed.
Siri will look for patterns in a user’s donated intents and will periodically attempt to provide a relevant suggestion. By defining different parameter combinations for the custom intent, Siri is able to look at trends in order to make recommendations.
It is thus important to delete donated intents that include irrelevant information that the user has removed from the app. If a user deletes a piece of data, they shouldn’t then receive a suggestion related to this information.
It’s also important to remove actions that the user hasn’t yet performed and we must remember to not donate intents for shortcuts while they’re being handled in the Siri Intent Extension. INInteraction encapsulates the information that Siri needs to suggest an intent. After a user completes an action, the app can then create this object and donate it.
As we do with NSUserActivity, we should implement the continueUserActivity method in our AppDelegate when handling our intent.
However, if we only implement continueUserActivity our shortcut won’t work in the background or work in line with Siri.
We therefore need to create an extension to handle the shortcut in the background. In order to do that we need to create a new target in our Xcode project and choose the Intents extension template. We then need to make our Intent handler conform to the protocol.
Before confirming, we should check all of the values and properties of our intent to make sure that they are valid and capable. If they’re not valid, we should return an error code. During the handle stage, we need to perform our shortcut and return a response object indicating the outcome – e.g success, failure, etc.
For the best user experience, we always need to implement continueUserActivity – even if we have implemented an Intents extension. This gives users the option to open the shortcut to our demo app via Siri, by tapping on the card on that shows our app’s custom UI.
In addition to donating intents that the user has performed, apps can now share customizable and relevant shortcuts for Siri to recommend at appropriate times in the future.
This means apps can now attach information about when or where this action might be useful to recommend, thus making our job a little easier!
Apps now have the ability to prompt the user to define a custom invocation string for a specific shortcut.
This allows users to put an app’s actions into their own vocabulary, creating a personable and intuitive interface. If this is something that you’d like to utilise, present INUIAddVoiceShortcutViewController to prompt the user for a custom shortcut statement.
This view controller takes a INShortcut object that represents the shortcut that needs to be configured.
Apple have made it really simple for developers to test their Siri Shortcuts.
Go to the Settings menu on your iOS device and click on the ‘Developer’ section. Scroll to the bottom and you’ll find the following two options that you’ll need to mark accordingly:
Once you’ve turned these settings on, you’ll be able to see all of your recent shortcuts and donations.
Apple has made it pretty easy for developers to utilise the Shortcuts function. Shortcuts, in turn, promise to make apps more responsive and they also offer a much more engaged user experience. Will you be giving them a go?
Siri Shortcuts was one of our highlights from the WWDC 2018 conference and we can’t wait to use it to its full potential during future projects. Got a project you’d like to talk to us about? Email us at: firstname.lastname@example.org.
PLEASE NOTE: This article has been written in reference to the Beta version of iOS 12.
If you’ve yet to check out the first post in this series, on how to create Siri Shortcuts, head over there now and give it a read.
(Image credit: 9to5mac)