A Longstanding wish for all iOS developers that now SiriKit can be integrated with our app services which handle’s app services through app requests. Whilst, Apple has made a leap towards being accomplish to get our tasks done through speaking and Undoubtedly, SiriKit can help  with that.

First and Foremost,  How SiriKit works?  How to integrate SiriKit with third parties app would be worth sharing. Well, SiriKit holds within Intents and Intents UI Framework.The Intents framework is the fundamental component that enables your application to integrate with Siri, whereas Intents UI enables you to customize the way your integration is displayed within Siri’s UI. SiriKit can only be integrated in VoiP Calling, Messaging, Payments,Lists and Notes, Visual Codes, Photos, Workouts, Ride Booking, Car Commands, CarPlay, Restaurant Reservation. In this post, we’ll focus on adding the basic integration and then add our custom UI in a following post(WorkOut Demo with Siri).

Getting Authorization  of Sirikit in App:

we’ll need to request authorization from the user, which will present them with an alert allowing them to accept or decline our request to integrate with Siri and Don’t forget to import Intents and it will give a Popup Message showing to enable SiriKit for our app.

Before Enabling SiriKit to our App, We need to write its usage description for its permission, with the key NSSiriUsageDescription The value for the key should be a string that describes what information is going to be shared with Siri.

NS-Siri-Usage-Descriptionthus it will show when dialog box appears.

Next, we need to prompt the user for permission. Your app might have a setting to turn this on so that you can prompt the user only when the feature is enabled. For this app, we will request as soon as the app finished launching. In AppDelegate, add import Intents to the list of import statements. Then, in application(_: didFinishLaunchingWithOptions:) add the following code or you can add in you ViewController file of viewdidloadMethod()

Here, I have integrated Siri for Workouts aso I have Mentioned names  related to Workout

Enabling Siri

The First Thing to turn On Siri is to turn On Siri in  “Capabilities” tab. There you can turn on Siri:

Creating Siri Extension

Next, we need to create an app extension. This is where all interactions with Siri take place. Create a new target and select “Intents Extension” as the type.

Give it an appropriate name, such as “Intent Handler “. By keeping the “Include UI Extension” checkbox selected, we will have an opportunity to customize the Siri interface if we so choose. If you look at the list of targets, you will now see three. They correspond to the iOS app, the intents extension, and the UI extension that is used for customization.

Specifying Intents for Siri :

The extension’s Info.plist file lets SiriKit know which intents are supported. Next, we will be working in this file. Do not confuse this file with the Info.plist file that we were previously working on. This Info.plist file will be found under the folder that was created in Xcode for your new extension. The file should look something like this:


Take note of two keys: “IntentsSupported” and “IntentsRestrictedWhileLocked”. These are both arrays of strings. Each string should match a class name. In the “IntentsSupported” array, we need to list the intents that we are going to support. In the “IntentsRestrictedWhileLocked” array, we list the intents that we want the user to unlock the device before using. We would want that if there is a security concern with the data our app holds. For example, apps that support financial transactions would require the device to be unlocked before committing a transaction.

We need to remove the default intents that are listed and add the ones that we actually want to support. After removing the three supported intents under “IntentsSupported”, add the new supported intent: “INCreateNoteIntent”.

There are two important things to mention at this point. First, there are more intents that we could support with our notes app, “INSearchForNotebookItemsIntent” and “INAppendToNoteIntent”. We are choosing not to support these. Second, the order of the intents is, sometimes, important. If, when speaking to Siri, the user is not clear about exactly which action is desired, Siri will use the first intent in the list that might work.

By default, it would give us an Intent Extension of Messaging but remove that and give custom Intents of your own.

Setting Intent Handler for Siri:

After Creating a Siri Extension, a two folders will be created

named ‘IntentHandlerUI’ and ‘IntentHandler’ (whatever name you have mentioned for creating SiriExtension as shown below:

In the Intent Handler folder, you will see ViewController file and its info.plist file. Please, don’t  get confuse between all info.plist file which was previously  created. Write your Code for handling Intents. Remember, this class is only created for handling Message Intents and it has method called handle , don’t forget to add a delegate called  INStartWorkoutIntentHandling, INEndWorkoutIntentHandling.

Thus, run the app, and You will get your output.  Before running Siri in your phone don’t forget to enable Siri for your app.  That’s It.

Enjoy , making your own Siri integration with your app.

 

Want to work with us? We're hiring!