Quick win for your app: “Add to Siri” Button

Giuseppe Travasoni
3 min readNov 21, 2018

As you already know since iOS 12 it’s possible to “donate” Siri Shortcuts from your app to the OS that users can find in iOS settings and that can be used in Shortcuts app for custom purposes.
You can find plenty of guides and talks about donating Shortcuts both using NSUserActivity or INIntent like this talk by Ellen Shapiro from NSSpain 2018.

In this post my topic is slight different and it’s a kind of quick win for Siri Shortcuts: show the UI for Siri Shortcut voice registration directly from your app with a standard button that every user can recognize.

IntentsUI inside SiriKit provides a new UIButton subclass with default user interface that can present an INUIAddVoiceShortcutViewController. This controller guides the user through the process of adding the shortcut to Siri instead of adding the shortcut to Shortcuts app and to device settings’ Siri section. First of all you have to import IntentsUI in your ViewController.
This button has 4 possibile styles that should be passed as parameters in init method:

Different styles

Note: INUIAddVoiceShortcutButton with style initializer comes with frame zero so if you init it programmatically remember to set button.translatesAutoresizingMaskIntoConstraints = false

Next step: create an INShortcut and set it into the button property. Touch up inside on INUIAddVoiceShortcutButton will automatically invoke actions on this shortcut.

A shortcut can be created with both an INIntent and an NSUserActivity. If you choose to work with INIntent remember that the initializer is failable.

Learn more about custom INIntent in the talk linked in the first paragraph or in this page of Apple Documentation.

Now it’s time to setup the delegate. INUIAddVoiceShortcutButtonDelegate protocol implementation will inform your view controller when it’s time to present a request view controller.

In this example I’m just presenting addVoiceShortcutViewController or editVoiceShortcutViewController without any other action, but you can check which button has called the delegate method or do the action that you prefer (I suggest to use those methods to track on your analytics service these actions).

Note: there’s no “edit button” so will be the button that will choose which view controller to present

It’s not over, we have one last point missing: INUIAddVoiceShortcutViewController and INUIEditVoiceShortcutViewController must have their own delegates because like many other system ViewControllers (i.e. mail sharing) they does not dismiss programmatically on cancel or success.

We have to handle two more delegates, but we can just discard the VC in any case like I’m doing in this example or like before use those methods to do any collateral action. Is interesting to trace also this events to understand better what your user is doing with Siri Shortcut.

Method signature are very talking about the action that the user has taken and please note that I’ve added delegate to previous methods:

Now your app is ready to donate and register voice shortcut in Siri without leaving it and without interrupting user experience flow.

When your users will say that “magic” phrase during a Siri conversation your AppDelegate will invoke continueUserActivity method as it will do for any Intent or NSUserActivity. You can find more about this topic in Apple documentation or in this tutorial about Handoff by Ray Wenderlich remembering that Handoff and Siri Shortcuts are both based on NSUserActivity and have the same entry point in the App Delegate.

You can also find a working example using the sample code of this article’s snippets here on my GitHub.

--

--

Giuseppe Travasoni

Co-Founder at TrueScreen // Co-Founder at Beatcode // iOS Developer and Architect