A Short Tutorial on How to Implement Voice Control Features in iOS Apps Using SiriKit
- 19528 views
- 7 min
- May 23, 2019
Voice control is no longer a futuristic technology. It’s a reality. If you’re an iOS developer who wants to keep pace with the times, you need to consider how voice control can enhance your apps and make them more competitive. In this tutorial, we show you how to implement different voice control features using SiriKit for iOS 12.
What is SiriKit?
SiriKit is a set of tools that allow iOS and watchOS apps to work with Siri, a virtual assistant that responds to a user’s voice. Siri helps users perform different tasks using only voice commands, even from the lock screen.
SiriKit lets you connect your app to Siri and use it even when the app isn’t open. This is possible if the app registers an extension with certain domains and intents that it can process. For example, an app for message communication will support the Message domain and the intent to send a message. Siri processes all interactions with a user and works with an extension for processing user queries.
SiriKit supports two types of extensions:
- An Intents app extension receives user requests from SiriKit and transforms them into app-specific actions. A user can ask Siri to book a flight, for instance.
- An Intents UI app extension displays content in the Siri interface after your Intents app extension complies with a user request.
Developers have access to the following domains:
- Lists. Create reminders, update to-do lists, and check shopping lists.
- Visual codes. Display QR codes that can be scanned with other devices.
- Ride booking. Allow users to book a ride and get a report about its status.
- Messaging. Send text messages.
- Photo search. Allow users to search for images and videos by content type and play a slideshow.
- Payments. Allow users to send and request payments.
- VoIP calling. Perform video and audio calls.
- Workouts. Allow users to start, pause, and finish routines.
- Climate and radio. Designed especially for the CarPlay system, this domain allows users to set up climate control and radio.
- Restaurant reservations. Allow users to create and manage restaurant reservations.
What’s new in SiriKit for iOS 12?
The main update with the new version of SiriKit is a shortcuts feature. Shortcuts allow users to quickly perform various tasks in the app from the lock and search screens and from the Siri watch face. The Shortcuts app allows users to create their own custom shortcuts and activate them with their voice.
Siri learns about new shortcuts via donations, which are released when a user performs an action in an app. How does it work? When a user orders something online, the app makes a donation for a quick order. Siri then decides when to offer shortcuts based on the frequency of donations, the user’s location, the current time, and other parameters.
There are two objects that an app can use to make donations:
- NSUserActivity – Donates a shortcut using a user activity, which involves viewing specific content in an app.
- INInteraction – Donates a shortcut using an interaction when the action involves a task that a user has accomplished with the app.
In this tutorial, we’re going to create a fitness app with a customized user interface that will respond to voice commands using SiriKit to start and finish a session. We’ll also add a nutrition ordering feature using Siri Shortcuts.
Let’s start with implementing voice commands using SiriKit to start and finish a workout in our fitness app.
Step 1. Integrate SiriKit
Step 1.1. Create an Intents Extension target and implement a handler to start a workout
First of all, we need to create a target of the Intents Extension type. Then in the Intents Extension target, we need to create a class that adopts the INStartWorkoutIntentHandling protocol. We have to implement the method func handle(intent: INStartWorkoutIntent, completion: @escaping (INStartWorkoutIntentResponse) -> Void), which has to return a final response after resolving all necessary values. This protocol allows us to add and resolve such values as the name of a workout and its purpose (for instance, 5 miles, 15 minutes, 100 kilocalories). All the other protocol methods are optional; they resolve values that depend on a domain.
Step 1.2. Implement a handler to finish the workout
Now it’s time to create a class that adopts the INEndWorkoutIntentHandling protocol and implements a method that has to return a response: func handle(intent: INEndWorkoutIntent, completion: @escaping (INEndWorkoutIntentResponse) -> Void) in the Intents Extension target.
Step 1.3. Determine handlers in the Intent Handler
Now, let’s override the func handler(for intent: INIntent) -> Any? method in the Intents Extension target in the IntentHandler class to determine what class handler is going to be used for the relevant intent.
Step 1.4. Authorize Siri and process intents in the application
Let’s implement the func application(_ application: UIApplication, handle intent: INIntent, completionHandler: @escaping (INIntentResponse) -> Void) method in the AppDelegate class to process a certain intent in the app launched after an interaction with Siri. To use Siri, we need to add an authorization query to the file.
Step 1.5. Use the intent instance to get values from a user
Implement the public static func start(with intent: INStartWorkoutIntent) method in the shared core target in the class WorkoutService. This will start a workout using data from the intent.
Below, you can see the results of implementing the voice command in our app.
Step 2. Implementing Siri Shortcuts
It’s time to implement a nutrition ordering feature using Siri Shortcuts.
Step 2.1. Create Intent Definition file
Create Intent Definition file in the shared core target. In this file, we need to create a customized intent with a certain category, parameters, and types of shortcuts.
Step 2.2. Implement a handler for product ordering
Create a class that adopts the OrderProductIntentHandling protocol in the Intents Extension target.
Step 2.3. Determine the handler in Intent Handler
Now, we need to override the func handler(for intent: INIntent) -> Any? method in the Intents Extension target in the IntentHandler class. That’s how we’ll find out what class handler will be used for the relevant intent.
Step 2.4. Donate an order to Siri
We also need to implement the private static func donate(_ order: Order) method in the shared core target. This method will donate after successfully placing an order.
Step 2.5. Create Intents Extension UI target and implement a custom UI
The next step is to create an Intents Extension UI target. Then implement your customized UI in the Intents Extension UI target, in the file MainInterface.storyboard. After that, implement the logic of displaying order's data in the IntentViewController class.
You can see the results of implementing Siri Shortcuts in the screenshots below.
SiriKit is a unique toolset that allows you to easily implement voice control features in your iOS apps. We hope that this tutorial has helped you improve your skills and hope we’ll see a lot of great projects based on SiriKit from you soon.
Check the source code of this tutorial and don’t hesitate to start a conversation below if you have any questions. Don’t forget to subscribe to our blog to get more detailed tutorials from us!