Teaching Siri new vocabularies at runtime

The second way you can teach Siri about specific content in your app is through the INVocabulary class. The INVocabulary class is used to teach Siri information that is specific to the user or changes over time.

A great example of user-specific vocabulary is a workout program that your user manually creates inside of your app. Or maybe the user's friends in a messaging app, if those friends don't match the people your user has in their list of contacts on their device.

Updating the vocabularies for an app always occurs in batches. You don't add or remove individual words or phrases for a specific string type. For example, if your user adds a new workout type to a workout application, or if they add new content in a messaging application, you must update the entire vocabulary for the parameter that the vocabulary applies to. Since the Hairdressers app uses its own list of hairdressers, Siri should learn their names so the users can message them. Add the following code to the application(_:didFinishLaunchingWithOptions:) method in AppDelegate, and don't forget to import the Intents framework, as follows:

let hairdressers = NSOrderedSet(array: HairdressersDataSource.hairdressers)
INVocabulary.shared().setVocabularyStrings(hairdressers, of: .contactName)

By supplying the contact names vocabulary when the app starts, it's available to Siri as soon as possible, even before the app's first screen is loaded.

If a user logs out or does something else that makes their custom vocabulary redundant or invalid, you need to make sure you delete the entire user-specific vocabulary by calling removeAllVocabularyStrings() on the shared INVocabulary class. This method of teaching vocabulary to Siri is not intended for vocabularies that are common to all users. If a vocabulary is the same for all users of your app, you should provide this vocabulary through the .plist file that was mentioned earlier. Now that you're completely up to speed regarding intents and vocabularies, let's see how you can start to handle the intents that Siri sends your way.

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

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