Swift Siri Shortcuts Tutorial
In this tutorial we will look at one of the newer features in iOS 12, Siri shortcuts. With Siri shortcuts you can make it so a user can easily use Siri to run a custom action in your app! For example you can let Siri order a Pizza, if your app was all about Pizza delivery, pretty neat! In addition the shortcuts can also be accessed by the spotlight search, and the control center will also suggest some for the user based on when they use it and the frequency.
For example you might order a Pizza at 5pm every Friday, so Siri’s smarts will figure this out and automatically suggest it to you!
Setup
Create a new single view application called Siri Shortcuts. First of all we need to make sure we remember what our Bundle Identifier is. This is a unique way of identifying that this app is ours, so any shortcut’s we make can be processed by our App. This can be found under the targets for the App, ours is “seemu.SiriShortcuts”:
Then select the “Capabilities” tab and enable the “Siri” toggles, this gives our app permission to use Siri Shortcuts. After this go to the storyboard and set it up as follows”
- Add a button with the text “Order Pepperoni Pizza”
- Add a label under the button
- Connect the label up to the ViewController.swift code as an outlet with the name “lblOrder”
- Connect the button up to the ViewController.swift code as a function with the name “orderPizza”
Then in the orderPizza function add the following code:
@IBAction func orderPizza(_ sender: Any) { print("Order Pizza") let activity = NSUserActivity(activityType: "seemu.SiriShortcuts.orderPizza") activity.title = "Order a pizza" activity.isEligibleForSearch = true activity.isEligibleForPrediction = true self.userActivity = activity self.userActivity?.becomeCurrent() }
Every time this function is called it will create a new activity for ordering the pizza and make it the current activity. Any action in an app can have an activity, and any app can have many activities.
Important note: Each activity will need a unique identifier – ours is “seemu.SiriShortcuts.orderPizza”
Overtime iOS will learn as you use an app as to what the most common activities are. It will then suggest them to the user to create Siri shortcuts to activate them, neat!
As an added bonus for this activity we have enabled it to show up in the spotlight search, you may enable / disable this as you wish.
Resuming the activity
Next up we need to head over to AppDelegate.swift, and tell our app what to do when it is launched from an activity from spotlight, or Siri.
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool { print(userActivity.activityType) // "seemu.SiriShortcuts.orderPizza" if(userActivity.activityType == "seemu.SiriShortcuts.orderPizza") { if let vc = window?.rootViewController as? ViewController { if let lblOrder = vc.lblOrder { lblOrder.text = "Ordering a Pepperoni Pizza" } } } return false }
When the app is launched from an activity this function will be run. First of all we print out the activity type to the console – recall that this is our unique identifier for the activity (“seemu.SiriShortcuts.orderPizza”).
The activity identifier is checked, if it is our order Pizza we will get the rootViewController (The default view controller that is loaded when we launch the app), and cast it as our ViewController with the Pizza label in it.
After we have this, we set our label text to ordering a pizza. If this app actually did order Pizza this is where your logic would go, or you could simply use a Dominos Pizza API to do it for you!
Testing the activity
Before we use the app, we need to do some pre setup for testing. Run the app, but in the simulator or on your device go to the settings, and select developer. Scroll down until you see ‘Display Recent Shortcuts’ and enable it. In iOS Siri will only suggest shortcuts for the most commonly used items, this setting will allow you to test any shortcuts that have recently been used.
Now that this is done, go into the app we made, and tap the ‘Order a Pizza’ button a bunch of times, this will register it as an activity in order so iOS will suggest it as a shortcut to us.
Now this is done head back into the Settings, and this time select Siri. In here you will see your shortcut – ‘Order a Pepperoni Pizza’. Recall this is the title we set for our activity.
To add it, simply tap plus. Siri will then record a voice shortcut for this, you will need to use a physical iOS device to test this part out. I simply recorded my self saying ‘feed me’. Now to test it out I simply say ‘Hey Siri feed me’. The app will then be launched and set our label to ‘Ordering a Pepperoni Pizza’, neat!
In addition you can use spotlight (Swipe left on the home screen) and search for ‘Order a pizza’. You will see our shortcut come up here also! Recall that we enabled it in our code to also show up in the spotlight search.