Add Siri Support to Your iOS App in Minutes Using Swift

It’s easy, and it will make your users happy

Yalçın Özdemir
AppNation Engineering

--

Photo by Fikri Rasyid on Unsplash

It’s interesting that not many apps support Siri even though it’s really easy to add to your app. I created this tutorial for anyone to easily add Siri support and make your users happy by allowing them to use the Shortcuts app to automate their life. Also, Apple likes developers who implement native functionality into their app — maybe you’ll get featured on the App Store after adding the Siri support into your app.

Open your Xcode project and go to Capabilities under your target. Scroll down to the bottom and enable Siri.

After enabling Siri, go to the General tab and scroll down to Linked Frameworks and Libraries. Click on the + icon and add Intents.frameworks and IntentsUI.framework.

After adding the frameworks to your project, open your Info.plist file in your project. You’ll need to add all the actions you want Siri to perform under the NSUserActivityTypes dictionary.

In this case, I added ShowNotifications under the NSUserActivityTypes dictionary and set the value as [my-app-bundle-id].actionname.

Now open your main view controller or the view controller where the Siri action will happen. Import IntentsUI framework.

import IntentsUI

In your view controller class, add a function called setupIntentsForSiri. CreateNSUserActivity and assign it to your view’s userActivity.

The process of configuring NSUserActivity is so simple. Set a title, add speech suggestion for the user in userInfo, and finally, use becomeCurrent to activate the action. The Siri shortcuts feature is only available in iOS 12.0 and after, so you will need to add #available if your project target version is under 12.0.

Now open your AppDelegate.swift file and add this function to trigger an action after the app is opened by Siri.

After making sure that the received userActivity is the one we want, I triggered NotificationCenter to post a notification, so I can let all my view controllers know that this action has happened and make the changes accordingly. You can do it this way or just grab your rootViewController using the window in your AppDelegate.

I need to add a Notification listener in my view controller to receive this notification and perform some action.

We also need to define the displayNotification function. Inside the function, we can perform the action we want to do. It can be anything you want: display a view controller or reload data, etc. I am not going to get into that part because it depends on what you’re implementing.

The last thing we want to add is the Shortcut view controller, to allow the user to record their voice and add the actual Siri shortcut. To do this, we need to present INUIAddVoiceShortcutViewController with the userActivity we created earlier.

You need to call displaySiriShortcutPopup function in your viewDidLoad to test it. However, Apple requires that developers only ask the user to add the Siri shortcut if the user has already performed the action in the app manually.

Example: If the user is able to order soup in the app, the Siri shortcut pop-up needs to be displayed after they have ordered soup.

Now test it by speaking to your phone like a crazy person: “Hey Siri… Siri?”

--

--