Siri integration on iOS 10 – Swift Tutorial
This tutorial written on June 13th, 2016 using the Xcode 8 Beta 1, and is using the Swift 3.0 toolchain.
Get Xcode 8 set up for iOS 10 and Swift 3 compilation.
If you have not yet downloaded Xcode 8 Beta 1, please do so here .
(Optional) Compiling from the command line
To opt in to the Swift 3.0 toolchain you shouldn’t need to change anything unless you want to build from the command line. If you plan to build from the command line, open Xcode-beta and from the OS menu bar select Xcode > Preferences . Then select the Locations tab. At the bottom of the page here you will see “Command Line Tools”. Make sure this is set to Xcode 8.0.
Now if you navigate to the project directory containing the .xcodeproj file, you can optional compile your project by calling
xcodebuild from the command line.
(Optional) Migrating from an existing Swift 2 app
If you are working with an existing Swift 2 project and want to add Siri integration with Swift 3.0, click on the root of your project and select Build Settings . Under Swift Compiler – Version , find the field labeled Use Legacy Swift Language Version and set it to No . This will lead to compiler errors most likely that you will need to fix throughout your project, but it’s a step I recommend to keep up with Swift’s ever-changing semantics.
Getting started with SiriKit
First, in your app (or in a new single-view Swift app template if you are starting fresh), switch to the general view by selecting the root of your project. Under this tab you can click the (+) icon in the lower land corner of the side-pane on the left. From the dropdown that appears selection iOS > Application Extension , and then select Intents Extension .
This adds a new intent to the project, and we’ll use it to listen for Siri commands. The product name should be something similar to your app so it’s easy to identify, for example if your app is called MusicMatcher , you could call the Product Name of this intent MusicMatcherSiriIntent . Make sure to also check the checkbox to Include UI Extension . We will need this later in the tutorial, and it’s easiest to just include the additional extension now.
What we’ve created are two new targets as you can see in the project heirarchy. Let’s jump in to the boilerplate code and take a look at the example in the IntentHandler.swift file inside of the Intent extension folder. By default this will be populated with some sample code for the workout intent, allowing a user to say commands such as “Start my workout using MusicMatcher”, where MusicMatcher is the name of our app.
Run the Template App as-is
It’s helpful at this point to compile this code as-is and try out the command on an actual iOS device. So go ahead and build the app target, by selecting the app MusicMatcher from the Scheme dropdown, and when the target device set to your test iOS device, press the Build & Run button.
You should see a blank app appear, and in the background your extensions will also be loaded in to the device’s file system. Now you can close your app using the Stop button in Xcode to kill the app.
Then, switch your scheme to select the Intent target, and press build & run again.
This will now prompt asking which app to attach to, just select the app you just ran, MusicMatcher . This will present the app again on your device (a white screen/blank app most likely), but this time the debugger will be attached to the Intent extension.
You can now exit to the home screen by pressing the home button, or the app may exit on it’s own since you are running the Intent and not the app itself (This is not a crash!!!)
Enable the extension
The extension should now be in place, but we as an iOS device user still may need to enable the extension in our Siri settings. On your test device enter the Settings app. Select the Siri menu, and near the bottom you should see MusicMatcher listed as a Siri App. Make sure the app is enabled in order to enable Siri to pick up the intents from the sample app.
Testing our first Siri command!
Try the Siri command. Activate Siri either by long pressing the Home button, or by saying “Hey Siri!” (note the “Hey Siri!” feature must be enabled in the settings first)
Try out some of the command “Start my workout using MusicMatcher”.
“Sorry, you’ll need to continue in the app.”
If you’re like me this will bail with an error saying “Sorry, you’ll need to continue in the app.” (For some reason this occassionally was not a problem. Ghosts?)
In the console you may see something like this:
: image not found
ended with exit code: 1
We need to add the CoreLocation library to our main project, to make sure it gets copied in with our compiled Swift code.
Select the project root again and then select your main MusicMatcher target. Here under General you’ll find area area for Linked Frameworks and Libraries . Click the (+) symbol and add CoreLocation.framework. Now you can rebuild and run your app on the device, then follow the same steps as above and rebuild and run your intent target.
Finally, you can activate Siri again from your home screen.
“Hey Siri!”“Start my workout using MusicMatcher”
Siri should finally respond, “OK. exercise started on MusicMatcher” and a UI will appear saying “Workout Started”
How does it work?
The IntentHandler class defined in the template uses a laundry list of protocols:
First an foremost is
INExtension which is what allows us to use the class as an intent extension in the first place. The remaining protocols are all intent handler types that we want to get callbacks for in our class:
The first one is the one we just tested,
If you command-click this protocol name you’ll see in the Apple docs this documentation:
@brief Protocol to declare support for handling an INStartWorkoutIntent
@abstract By implementing this protocol, a class can provide logic for resolving, confirming and handling the intent.
@discussion The minimum requirement for an implementing class is that it should be able to handle the intent. The resolution and confirmation methods are optional. The handling method is always called last, after resolving and confirming the intent.
Or in other words, this protocol tells SiriKit that we’re prepared to handle the English phrase “Start my workout with AppName Here .”
This will vary based on the language spoken by the user, but the intent will always be to start a workout. The
INStartWorkoutIntentHandling protocol calls on several more methods, and they are implemented in the sample code. I’ll leave you to learn more about them if you want to build a workout app, but what I’d rather do in the remainder of this tutorial is add a new intent handler for handling the sending of messages.
Let’s Add a New Message Intent
Now that we’ve confirmed that works, let’s move on to adding a new type of intent for sending messages. The docs here show the following:
So let’s add the
INSendMessageIntentHandling protocol to our class. First we’ll just specify we want to use it by appending it to the list of protocols our class adheres to in IntentHandler.swift. Since I don’t actually want the workout intent’s, I’ll also remove those, leaving us with just this for the class declaration:
If we just left it at that we wouldn’t be able to compile our code since we stil need to implement the required methods from the
Again, if you ever need to check what those methods are, just command+click the text
INSendMessageIntentHandling and take a look at what method signatures are present that are not marked with the
In this case we find only one required method:
@brief handling method
@abstract Execute the task represented by the INSendMessageIntent that's passed in
@discussion This method is called to actually execute the intent. The app must return a response for this intent.
@param sendMessageIntent The input intent
@param completion The response handling block takes a INSendMessageIntentResponse containing the details of the result of having executed the intent
, completion: (
Adhering to the new Message Intent protocol
So back in our IntentHandler.swift, let’s add a line seperator (useful for navigating code with the jump bar)
// MARK: - INSendMessageIntentHandling
Underneath this MARK, we can implement the function. I find it’s most useful with Xcode 8 to simply begin typing the method name, and let autocomplete take it from there, choosing the relevant option.
In our handler, we’ll need to construct an
INSendMessageIntentResponse in order to call back the completion handler. We’ll just assume all messages are successful here and return a success value for the user activity in the
INSendMessageIntentResponse constructor, similar to how this is done in the template app. We’ll also add a print statement so we can see when this handle method is triggered by a Siri event:
, completion: (
"Message intent is being handled."
(code: .success, userActivity: userActivity)
Adding the intent type to the Info.plist
Before this app will be capable of handling
INSendMessageIntent , we need to add the value to our Info.plist. Think of this as something like an app entitlement.
In the Info.plist file of the intent , find and expand the
NSExtension key. Then extend
NSExtensionAttributes , and then
IntentsSupported under that. Here we need to add a new row for our
INSendMessageIntent to allow the app to process Message intents.
Testing the new intent
Now that we’ve got our new intent set up, let’s give it a try. Recall that you must build the app, run it on the device, and then run the extension in order to debug the extension. If you don’t do run in this order the extension will either not work, or it will not log to the Xcode console.
Try calling upon our intent in Siri, and you will now see a new message window appear! The window is pretty empty, and there isn’t much logic to tie in to our app just yet. We need to implement the remaining callbacks and add some of our app’s messaging logic to provide a better experience. We’ll cover that in Part 2, which is coming soon. If you want me to email you about it when the next part is out, sign up for my newsletter to get the scoop .