Siri Shortcuts Tutorial in iOS 12

In this iOS 12 tutorial, you’ll learn how to build Siri Shortcuts for your app to surface in Spotlight as well as command Siri with your voice. By Zaphod Beeblebrox.

3.9 (20) · 2 Reviews

Download materials
Save for later
Share
You are currently viewing page 2 of 4 of this article. Click here to view the first page.

Continuing a User Activity

Tap on the “Write a new article” result in your search. You’ll be taken to your app’s home screen.

Your feature may be exposed to the system, but your app isn’t doing anything when the system tells it the user would like to use the feature.

To react to this request, open AppDelegate.swift, and at the bottom of the class, add the following method definition:

func application(
  _ application: UIApplication,
  continue userActivity: NSUserActivity,
  restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void
) -> Bool {
  return true
}

Inside the method and before the return statement, create the New Article view controller and push it onto the nav stack:

let vc = NewArticleViewController()
nav?.pushViewController(vc, animated: false)

Build and run again. When you search for this feature and tap on it, your app takes you directly to the New Article screen.

Developer Settings for Working With Siri

All you’ve done so far is accessed your feature from Spotlight. This isn’t anything new, and it would work even if Siri weren’t involved since you made your activity eligible for search.

To prove that Siri can start suggesting this action, you’ll need to go to the Settings app and enable a few options.

Open Settings and find the Developer option. Scroll towards the bottom and you’ll see a section named SHORTCUTS TESTING.

Turning on the Display Recent Shortcuts option means that recently donated shortcuts will show up in Spotlight Search instead of Siri’s current predictions.

Similarly, Display Donations on Lock Screen always shows your recent donations as a notification on your lock screen.

Turn on both of the options so you can always see what shortcuts your app has donated most recently.

Now that you know how to see what Siri’s suggestions look like, it’s time to turn these activities into full-blown shortcuts!

Turning User Activities Into Shortcuts

When a user wants to turn one of these activities into a shortcut, they can do so from the Settings app. As the developer, there’s nothing more you need to do.

To test it out: On your device, open Settings > Siri & Search.

The first section shows a list of shortcuts donated by the different apps on your phone. You’ll see the “Write a new article” shortcut in this list; if you don’t, tap More Shortcuts to see more.

To add a shortcut for this action, tap the action, and you’ll be taken to the shortcut creation screen.

Here, you can see that suggested invocation phrase you added earlier.

Tap the red circle at the bottom. When prompted, say to Siri, “Time to write”. After Siri figures out what you said, tap Done to finalize your shortcut.

Now, your shortcut is listed along with any other shortcuts you’ve created.

Adding Shortcuts To Siri From Your App

This is all well and good, but can you expect users to muck around in the Settings app to add a shortcut for your app? Nope! Not at all.

Fortunately, you can prompt your users to do this straight from your app!

Open NewArticleViewController.swift and you’ll see an empty definition for addNewArticleShortcutWasTapped().

This is the method that gets called when the blue “Add Shortcut to Siri” button is tapped.

The IntentsUI framework provides a special view controller that you can initialize with a shortcut. You can then present this view controller to show the same UI you just saw in the Settings app.

Add the following two lines to initialize the shortcut:

let newArticleActivity = Article
  .newArticleShortcut(thumbnail: UIImage(named: "notePad.jpg"))
let shortcut = INShortcut(userActivity: newArticleActivity)

Next, create the view controller, set the delegate and present the view:

let vc = INUIAddVoiceShortcutViewController(shortcut: shortcut)
vc.delegate = self

present(vc, animated: true, completion: nil)

At this point, Xcode complains that this class isn’t fit to be the delegate of that view controller. You’ll need to fix this.

Add the following extension at the bottom of the file:

extension NewArticleViewController: INUIAddVoiceShortcutViewControllerDelegate {
}

Then, conform to the INUIAddVoiceShortcutViewControllerDelegate protocol by adding the method for when the user successfully creates a shortcut:

func addVoiceShortcutViewController(
  _ controller: INUIAddVoiceShortcutViewController,
  didFinishWith voiceShortcut: INVoiceShortcut?,
  error: Error?
) {
}

Also, add the method for when the user taps the Cancel button:

func addVoiceShortcutViewControllerDidCancel(
  _ controller: INUIAddVoiceShortcutViewController) {
}

Next, you need to dismiss the Siri view controller when these methods are called.

Add the following line to both methods:

dismiss(animated: true, completion: nil)

Build and run to try it out! Go to the New Article view and tap Add Shortcut to Siri.

You’ll see a view controller that looks a lot like the view you see when you go to set up shortcuts in the Settings app. With this in place, your users won’t have excuses for not utilizing your app’s full potential! :]

Publishing Articles with Siri

Next, you want to add the ability to publish articles you’ve written directly from Siri. The big difference here is that instead of Siri opening your app, everything will happen in-line, directly from the Siri UI.

For this shortcut, you’ll create a custom Intent to define the back and forth between Siri, your users and your app.

Defining a Custom Writing Intent

In the Project navigator, locate the ArticleKit folder and click on it to highlight it.

Then, press Command + N to create a new file.

In the filter search box, type Intent and you’ll see SiriKit Intent Definition File.

add intent definition file

Click Next, then name your file ArticleIntents.intentdefinition.

Now it’s time to create an intent for posting articles you’ve written.

Go to the bottom of the section that reads No Intents and click the plus button to add a new intent definition.

add new intent

Name it PostArticle, and then configure your intent based on these settings (described after the picture):

PostArticleIntent definition

This screen is for configuring the information that the publish intent needs to work.

The options in the Custom Intent section define what type of intent it is and can affect how Siri talks about the action. Telling Siri that this is a Post type action lets the system know that you’re sharing some bit of content somewhere:

  1. Category: Post
  2. Title: Post Article
  3. Description: Post the last article
  4. Default Image: Select one of the existing images in the project.
  5. Confirmation: Check this box since you want to ask the user to verify that they’re really ready to publish this article.

The Parameters section is where you define any dynamic properties used in the Title and Subtitle, which you’ll do now.

Define a parameter named article that’s a Custom data type and a publishDate that’s of type String.

Then, in the Shortcut Types section, click the plus button to add one type that includes both the article and publishDate arguments as its parameters.

Next, set up the Title and Subtitle for the shortcut.

Make the title Post “${article}” and the subtitle on ${publishDate}. If you don’t copy and paste, make sure to let Xcode autocomplete article and publishDate.

Finally, make sure Supports background execution is checked so you aren’t forced to leave the Siri UI.