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 4 of 4 of this article. Click here to view the first page.

Wrapping Up

The final thing you need to worry about is deleting intents from the system. Let’s say a user has deleted the only article they wrote. If Siri prompts them to publish this article, that means the system remembers information that they wanted to delete.

Since this goes against Apple’s strict respect for user privacy, it’s your job to remove activities and intents that were deleted.

Go to ArticleFeedViewController.swift and scroll to the bottom of the file.

Then, add the following method call to the bottom of remove(article:indexPath:):

INInteraction.delete(with: article.title) { _ in
}

The completion block allows you to react to deletion errors however you see fit.

Since the new article shortcut doesn’t contain any user data, it isn’t strictly necessary to remove it.

Finally, open Layouts.swift and find the UITableViewDataSource extension for ArticleFeedViewController. Add the following at the end of the extension to enable deleting articles:

func tableView(_ tableView: UITableView,
                 commit editingStyle: UITableViewCell.EditingStyle,
                 forRowAt indexPath: IndexPath) {
  if editingStyle == .delete {
    let article = articles[indexPath.row]
    remove(article: article, at: indexPath)
    if articles.count == 0 {
      NSUserActivity.deleteSavedUserActivities(withPersistentIdentifiers: 
        [NSUserActivityPersistentIdentifier(kNewArticleActivityType)]) {
          print("Successfully deleted 'New Article' activity.")
      }
    }
  }
}

If you want, you can use NSUserActivity‘s class method deleteSavedUserActivities(withPersistentIdentifiers:completionHandler:) to remove all of the activities that were donated with a single identifier.

Where to Go From Here?

You’ve worked through a lot, but there’s still a lot to learn. You can download the completed version of the project using the Download Materials button at the top or bottom of this tutorial. Be sure you update your bundle id and app group id before you try to run it.

If you want to learn more about Shortcuts, check out the two WWDC videos from 2018. The first presentation covers a lot of the same material as this tutorial and is a good refresher to solidify the big ideas you learned here. The second goes more into best practices.

As always, we hope you enjoyed the tutorial. Let us know if you have any questions down in the comments!