iOS 12 Siri Shortcuts Part I

Sinan Ege
hepsiburadatech
Published in
5 min readOct 9, 2018
Siri Shortcuts

As you know Apple is trying to make it easier to use Siri. Every year introduces new capabilities to Siri and the last one is really useful. Please Welcome to the stage, Shortcuts! At WWDC 2018 Apple announced Siri Shortcuts which allows to define and access frequently used tasks. You can review summary of Siri Shortcuts from Apple’s website.

With iOS 12 or later, Siri Shortcuts let you quickly do everyday tasks, and with the apps you use the most — all with just a tap or by asking Siri.

Basically we can split shortcuts into two parts. The first one is using NSUserActivity to enable shortcuts.

Siri can predict shortcuts to actions that a user may want to perform using your app, and suggest those shortcuts to the user in places such as Spotlight search, Lock Screen, and the Siri watch face. Siri learns about the shortcuts available for your app through donations that your app makes to Siri.

So we can start with explanation of donate. Actually you are really donating some predefined tasks to Siri. That donated tasks will be used from Siri for suggests to users some shortcuts due to users usage of your application or users can use that donated tasks to define shortcuts.

Lets start with example project. Assume that you have a project which allows to users can keep number of drinks in a day. Users should tap the button when they drink something and our app will save counts of drinks in NSUserDefaults. We have 3 types of drink:

  • Water
  • Coffee
  • Tea

We should do some preparations before write any code for donate user activity.

  • First we have to enable Siri under Capabilities. This will enable Siri capability from bundle identifier like remote notification.
Capabilities Tab in Project Settings
  • After that we should add any NSUserActivity type to our Info.plist file. It should be array and its prefix should be same with your bundle identifier. We will use that value while donating activity to Siri.

Now we will ready to donate activity due to DrinkType. Write a method which takes DrinkType as parameter and creates user activity with raw value of it.

  1. First we will add this method to our view controller and we will call it from IBAction function. We will use it like self.donateUserActivity(with: .coffee).

Let’s look at the code we wrote in a little more detail:

  • We created NSUserActivity with activityTypeName which we defined in Info.plist file.
  • title: We set title of activity which will be used in Siri suggestion button.
“I just drank \(type.rawValue)”
  • userInfo: It will be used when user launch our app via Siri. If you look at the declaration of userInfo , you’ll see that you can use some kind of data types. Here is the discussion about userInfo from Apple.

Each key and value must be of the following types: NSArray, NSData, NSDate, NSDictionary, NSNull, NSNumber, NSSet, NSString, or NSURL. The system may translate file scheme URLs that refer to iCloud documents to valid file URLs on a continuing device.

  • isEligibleForSearch: It allows Siri to index this and use it for voice-matched queries.

A Boolean value that indicates whether the activity should be added to the on-device index.

  • isEligibleForPrediction: Siri uses machine learning to suggest activities to users so you are saying that okey you can use my donation for suggestions.

A Boolean value that determines whether Siri can suggest the user activity as a shortcut to the user.

Final step is when user launch app via Siri, your app should handle received data and process it due to condition. We have donated NSUserActivity so we will handle it in AppDelegate continue user activity method.

  • We should check equality of activityType with our predefined UserActivityType in Info.plist and it matches we can handle it.
  • This part of code is depends how will you handle userActivity in your app. You can route any view controller or send analytics data, it is up to you.
  • In this case first we are looking for rootViewController for check our ViewController.
  • Most important part is we are looking to userInfo’s of userActivity because we will get drinkType from there and do some kind of action related with drinkType. If there is drinkType, we are passing it to viewController. ViewController will take care of the rest.

Thats it! Congratulations!👏👏👏

Let’s Test Our Application

  • First run the app and tap each button once. It will donate for each type of drink to Siri.
  • Open Settings > Siri & Search > All Shortcuts. You will see our app’s shortcuts.
Donated Shortcuts of our application
  • Tap any one and record your voice to define shortcut. For example you can say “tea” for i just drank tea shortcut. You will see your defined shortcut under My Shortcuts section.
  • Open siri and say “tea”. Siri will launch our application and increase count of tea.
Second tea has triggered by Siri

Here is the link of github repository of project. Thanks for reading! 🚀

--

--