Configuring your extension

Now that you have added your extensions to the Hairdressers project, it's time to configure the app so it can integrate with Siri. Go through the following steps to properly configure your app:

  • Make sure you're using a paid developer team, and make sure the app uses this paid team by selecting it in the project settings.
  • Enable the Siri capability in the Capabilities tab.
  • Add the required Privacy key to the app's Info.plist file.

After configuring the project, the first step to integrate your app with Siri is to ask the user for permission to do so. Open AppointmentsViewController.swift and import the Intents framework at the top of the file as follows:

import Intents

You might have expected all of the Siri-related code to exist in the SiriKit framework. Instead, some parts of the Siri integration live in the Intents framework, because some other apps, like the Maps app on iOS, use intents to determine what action a user wants to perform.

After adding the import, add the following implementation for viewDidAppear(_:) to AppointmentsViewController:

override func viewDidAppear(_ animated: Bool) {
  super.viewDidAppear(animated)

  INPreferences.requestSiriAuthorization { status in
    print(status == .authorized)
  }
}

This preceding code asks for permission to use Siri in this app as soon as possible, just like Apple recommends. If permission is already granted, the permissions dialog won't be displayed, so there is no need to check for the current status prior to asking permission. For now, you have done all the required work in the app.

If you open the MessageHairdresserIntent folder that Xcode created when you added the extension earlier, you should see that there are two files in there: a file named IntentHandler.swift, and the app extension's Info.plist. The IntentHandler class is responsible for communicating with Siri to resolve, confirm, and handle the intents for which the extension is registered. The Info.plist is used to determine which intents can be handled by the 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 that can be handled by the extension. The IntentsRestrictedWhileLocked key specifies which of these supported keys can or can't be used without unlocking the device. SiriKit itself will lock some intents by default. Money transfers, for example, can't be done without unlocking the device, regardless of your extension settings:

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 transfers money into your bank account. Each of these intents has their own corresponding class that holds all of the properties that are used to describe the intent.

To enable message sending in Hairdressers, 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, you can keep any intents that you want to play around with, or add more, if you like!

Even though Siri is quite smart when it comes to resolving intents, some apps have their own terminology for specific actions. Custom terms make resolving intents a lot harder for Siri. Luckily, you can help Siri out by adding vocabulary information to Siri.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.13.113