The release of Siri Shortcuts gave more opportunities to both app developers and iOS users. This useful feature groups iOS actions that can be triggered with custom voice commands. In our article, we share a tutorial on how this works and how to add Siri Shortcuts support to your own app.

What shortcuts do

Siri Shortcuts is a feature in iOS 12 that allows installed apps to expose their functionality to Siri.

Apple announced Siri Shortcuts at WWDC 2018 in San Jose. Siri Shortcuts is a native application that enables quick access to features of other apps on iOS 12. Once a user creates a command for a supported action (playing music, checking the weather forecast, or booking a taxi, for instance), the Shortcuts app will access the feature without opening the app that offers it. This way, users can quickly perform common tasks with just one tap or by asking Siri.

Shortcuts connects to email accounts, calendars, and social media apps to predict messages you may want to send and apps you’re likely to use. Siri offers prompts for how to use your device based on your everyday activities.

Siri can also suggest specific shortcuts at the appropriate time. For example, you get to work by taxi and use a taxi app to request a ride every morning. If this app supports Siri Shortcuts, Siri will offer you a shortcut to quickly book your ride without having to go into the app.

Siri Shortcuts is a native application that enables quick access to features of other apps on iOS 12.

In iOS 12, shortcuts can be accessed in three ways:

  • As suggestions that predict what task you’d like to perform based on frequently used app
  • In Siri, invoked by a user’s custom voice commands
  • Using pre-built or custom shortcuts presented in the Shortcuts app Gallery (for those who don’t want to create their own shortcuts)

Brief story of Siri Shortcuts

Siri Shortcuts smoothly replaced the existing Workflow app (hence the numbering of the initial version as 2.0) and inherited most features that Workflow had on iOS. Since its first release in September 2018, Siri Shortcuts has received two major updates.

Workflow was developed in 2014 as a scripting application for iOS.

In version 2.1, Apple integrated actions such as weather forecasts, alarm settings, and a unit converter that can be used as part of a shortcut. The Get Last Import action provides the most recently imported photos from the Photos app. In addition, the update made it possible to automatically play music on HomePod when a user runs a shortcut using Siri.

Version 2.2 added actions for the Notes app. They allow users to create notes, attach files to them, find notes, and open specific notes. The Get Number from Input action extracts numbers out of text, while tapping the Library button will get you to the bottom of the shortcuts list. The Travel Time action was also tweaked a bit and now offers more details, such as route names, arrival times, and distances.

Siri Shortcuts inherited most features that Workflow had on iOS.

According to Apple, with the next release Shortcuts app will allow users to create sets of several shortcuts to manage multiple apps at once. This means that users will be able to check email, learn the latest news, and send a message by speaking one phrase to Siri or with one quick tap.

The new Shortcuts update might also offer iOS users personalized responses to calls they can’t take. Using a shortcut, users will be able to send a text message to the caller explaining that they can’t answer the phone at the moment.

How to integrate Siri Shortcuts into a third-party app?

The introduction of Siri Shortcuts in iOS 12 gave third-party developers new capabilities to expose their app’s services directly to Siri. Previously, the SiriKit SDK was limited to Apple’s own apps and certain domains. However, thanks to the release of Siri Shortcuts, developers can now create custom intents to represent any domain and extend Siri’s features within their own apps.

Shortcuts let you expose the capabilities of your app to Siri.

We’ve created a tutorial to show you how to integrate Siri Shortcuts within a third-party app. For educational purposes, we’ve built a simple project that will let users say the phrase “hello shortcut,” which will launch our demo app and present a UIAlertView.

Two ways to set up a shortcut

For starters, you should decide which features of your app are suitable for turning into shortcuts.
Ideally, they would be actions your users perform regularly. Once that’s decided, choose one of two ways to create a shortcut:

  1. NSUserActivity: User activities are included in the existing API that exposes the tasks a user can do to hand off app or provide Spotlight search. Note that this option is only useful when a shortcut is triggered with the help of Siri.
  2. Custom intents: Creating a custom intent will reveal the true power of shortcuts. With an intent, your users can interact with your app via Siri without ever having to open it. For this, you need to use the Intent App Extension, which will handle shortcuts running in the background.

To use Siri Shortcut in your project, you should have Xcode 10 and macOS Mojave.

However, creating a custom intent might add some extra work and expense, as you need to share logic between the main app and the extension. A much easier way to implement Siri support is using the NSUserActivity API.

Also note that to use Siri Shortcut in your project, you should have Xcode 10 and macOS Mojave. Otherwise, you won’t be able to run the code in this tutorial, as the Siri Shortcuts API was introduced only in Xcode 10 and iOS 12.

Creating a sample project

First off, run Xcode and create a new Single View App. Type in SiriShortcuts for the project name, or choose any other name to your liking. We’re using com.mobindustry as our Organization Identifier. Once done, click Create to load your Xcode project.

Next, navigate to the Project Settings section and select Capabilities. Scroll down and switch the toggle to enable Siri, as indicated in the screenshot below. Now you can use the Siri SDK in your app.

Once Siri is on, Xcode will add a .entitlements file to your project. After that, go to the General tab and select Linked Frameworks and Libraries. To add a framework, click the + button. Find Intents.framework and press Add. This will allow you to use the new Intents framework within your application.

 

The last preparation step is to navigate to the Info.plist file and add an NSUserActivityTypes dictionary with a key-value pair. The value of the first item has to include your bundle identifier with an attached action. In our case, it will be “HelloShortcut”.

Now, head to your ViewController.swift file. After the viewDidLoad method, create a new method called createShortcut( ). Next, perform the following operations:

     override func viewDidLoad() {
        super.viewDidLoad()
        
        createShortcut()
    }

    func createShortcut() {
        let activity = NSUserActivity(activityType: "com.mobindusrty.Siri-shortcuts.HelloShortcut") // 1
        activity.title = "Say Hello Shortcut" // 2
        activity.userInfo = ["speech" : "hi"] // 3
        activity.isEligibleForSearch = true // 4
        activity.isEligibleForPrediction = true // 5
        activity.persistentIdentifier =  NSUserActivityPersistentIdentifier("com.mobindusrty.Siri-shortcuts.HelloShortcut")
        view.userActivity = activity // 7
        activity.becomeCurrent() // 8
    }

Set up an instance of NSUserActivity and assign the activityType parameter to the identifier in your Info.plist file.

2) Define the activity title under which your shortcut will appear in Settings and in Spotlight search.

3) Add a userInfo dictionary. According to Apple, it should contain information needed to continue an activity on another device.

4–5) Set two properties – .isEligibleForSearch and .isEligibleForPrediction – to true. These two properties allow iOS to search and suggest your NSUserActivity on the device.

6) Set the persistantIndetifier property to an instance of NSUserActivityPersistentIdentifier assigned to your identifier from earlier.

7) Assign the view’s userActivity property to the activity you’ve just created and invoke the becomeCurrent( ) method to initiate your activity.

To understand how Siri Shortcuts work, create one more method called doSomething( ). This method will display UIAlertController after we open the app from Siri Shortcuts. Note that this function is public because we need to call it outside the scope of our Viewcontroller.

    public func doSomething() {
        let alert = UIAlertController(title: "Congratulation!", message: "App is open from Shortcut", preferredStyle: UIAlertController.Style.alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }

Now navigate to the AppDelegate.swift file and add an application(_:continueUserActivity:restorationHandler) function. This will expose our newly created activity within the app delegate and allow Siri to take action and launch our app if it isn’t opened.

    func application(_ application: UIApplication, continue userActivity: NSUserActivity, restora-tionHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
        let viewController = window?.rootViewController as! ViewController
        viewController.doSomething()
        return true
    }

Lastly, navigate to the Settings app and select Siri. There, you’ll see your new shortcut called Say Hello Shortcut. Add it by clicking the + button, then record your voice command to trigger it.

 

Now say your command to Siri to open your app and see the UIAlertController!

 

 

 

 

Wrapping up

In this article, we’ve provided a simple overview of how to integrate Siri Shortcuts into a third-party app. As an example, we set up a basic app to show you how Siri Shortcuts works with a custom shortcut. We used NSUserActivity in our tutorial, as this tool is perfect for small projects. As Siri Shortcuts offer endless possibilities for developers, it’s safe to predict that more and more apps will acquire Siri support in the near future.

iOS development services

Do you want your app to support Siri Shortcuts? We are ready to offer you a quick and high-quality solution.

Get a Free Consultation!
Contact Us

Contact Us

+