At WWDC 2018 Apple announced Siri Shortcuts, which allows developers to build experiences to quickly accomplish tasks from their lock screen. Users will be able to manage third party shortcuts using the “Shortcuts” app which will be available later when the iOS 12 is publicly announced.
In this post we will be covering how to create Siri Shortcuts using NSUserActivity. NSUserActivity is a great choice, if your Shortcut is designed to open the installed app. You can also use NSUserActivity to restore the state of the app, although this functionality is currently broken in iOS 12 Beta 4.
Create a new Single View Application project in Xcode and name it “HotCoffee”. The HotCoffee app will be responsible for ordering us hot coffee whenever you need it. The user interface design is completely up to you, we have taken the simplest approach possible as shown below.
In order for Siri to learn about our coffee drinking behavior, we need to donate our activities to Siri. The best time to donate the activity in our scenario is when the order is placed. The following code is used to donate your activity.
We start by creating the order activity. This is accomplished by using the NSUserActivity class and passing the activity type as a string. Don’t worry about activity type right now, we will see in a moment where to declare it.
Next, we make sure that our activity is eligible for search and eligible for prediction. This is going to help Siri learning more about our activity. We set the title of our shortcut using the title property. The suggestedInvocationPhrase is use to give user an idea about what phrase they should set for the Siri voice shortcut.
If you want to pass additional information from your activity to the iOS app you can put that information in userInfo property. In Xcode 10 Beta 4 the userInfo property does not work and always returns an empty dictionary.
Finally, you can set the view controllers userActivity property to the orderActivity and mark it current.
Before running your iOS app and donating the activity let’s make sure the activity type is set property in info.plist file. Add a new node “NSUserActivityTypes” as shown below.
One last thing! I promise :)
Launch your simulator and go to Settings -> Developer and scroll all the way down and make sure that both the options under “Shortcuts Testing” are enabled.
Awesome! Now you can run your application and place an order. As soon as you place an order the activity is donated and since the developer shortcut testing options are enabled you will be able to see your activity under Siri Suggestions (Just swipe down from the top to make your search bar visible).
Congratulations on making your first Siri Shortcut :)
You can also add this shortcut as a Siri voice shortcut. For this open Settings app in simulator and then select Siri. You should see your shortcut listing under Shortcuts section. Simply, press the “+” button to add your shortcut as a Siri voice shortcut. This means your shortcut will be launched by simply using Siri voice.
If you have enjoyed this post and want to learn more about Siri Shortcuts then check out my 3+ hour Udemy course “Mastering Siri Shortcuts in iOS”. The course goes into much more detail in how to create Siri Shortcuts using NSUserActivity, Intents, Custom Responses, Custom User Interface and much more.