Any time a user asks Siri for something, the user's query is resolved to an intent. Every intent has a number of parameters or variables associated with it. Siri will always attempt to fill in these parameters to the best of its ability. Every app that integrates with Siri should have an extension. This extension is used by Siri to make sure that all the parts of an intent are present and valid.
You might expect intents to be a part of SiriKit. In reality, they're not. Intents have their own framework in iOS, enabling other applications, such as Maps, to make use of intents as well. This means that any knowledge regarding intents that you will obtain in this chapter translates over to other extensions that make use of intents.
To enable Siri handles certain intents with your applications, you must specifically register for these intents in your extension's Info.plist file. In this book's repository, you'll find a project named SiriKitMessaging. We'll add extensions to this application in order to create a fake messaging app.
Before we explore the project, let's set it up so that it is able to integrate with Siri. First, make sure that the signing identity for the project is set to your own paid developer team. You can find and change your team on the General tab for your application target. Next, go to the Capabilities tab and enable Siri. Also, open the Info.plist file and add the NSSiriUsageDescription key to it. Provide a short description about what Siri is being used for in this app.
Finally, open ViewController.swift and add the following implementation for viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
INPreferences.requestSiriAuthorization { status in
print(status)
}
}
This snippet asks for permission to use Siri as soon as possible in this app, just such as Apple recommends. If permission is already granted, the permissions dialog won't be displayed, so we don't have to check for the current status prior to asking permission. Now that you're all set up to integrate your app with Siri, let's go ahead and explore the example project .
In the project, you'll find two extensions: MessagingExtension and MessagingExtensionUI. Whenever you add a new Intents Extension to your project, you're offered the option to add a UI extension as well. We'll look into these UI extensions later. Currently, we're only interested in registering our extension to be used for certain intents.
If you open the MessagingExtension folder, there are two files in there. A file named IntentHandler.swift and the extension's Info.plist. The IntentHandler class is responsible for communicating with Siri to resolve, confirm, and handle the intents we're registered for. The Info.plist is used to determine which intents can be handled by our extension.
Open the Info.plist file and expand the NSExtension key. You'll notice that there are two intent-related keys in the file: IntentsRestrictedWhileLocked and IntentsSupported. The second key contains all of the intents our extension can handle. The IntentsRestrictedWhileLocked key specifies which of these supported keys can or can't be used without unlocking the device. SiriKit itself will lock certain intents by default. Money transfers, for example, can't be done without unlocking the device, regardless of your extension settings:
![](assets/57eccd8b-be14-4482-90c5-93d22dd8c0f4.png)
The list of intents in IntentsSupported is a list of intent class names that your extension is able to handle. Xcode has added a couple of example intents, but this list is not even close to being an exhaustive list of available intents. For a complete list of available intents, you should have a look at the documentation for the Intents framework.
The available intents range from starting a workout to booking a restaurant reservation or requesting that another person transfer money into your account. Each of these intents has their own corresponding class that holds all of the properties that are used to describe the intent.
For our demonstration of integrating Siri into your app, only the INSendMessageIntent is required, so you can remove the other two intents. If you want to experiment with multiple intents from the get-go, go ahead and keep any intents you want to play around with. Or add more if you like.
Even though Siri is quite clever when it comes to resolving intents, some apps have their own terminology for certain actions. Custom terminology like this will make resolving certain intents a lot harder for Siri. Luckily, we can help Siri out by adding vocabulary information to Siri. The next section explains how you can do this.