Siri Integration in iOS

03 / Aug / 2016 by Kailash Narayan 2 comments

Introduction:  As we know that apple has launched Siri 5 years back, but only for user interaction not for iOS development point of view. Finally in iOS 10, apple launched Siri for development. Now developer can integrate Siri kit in his/her application to provide some predefined domains of services.

Some example where you can use Siri kit :

  1. If you want to book a ride then you need not to do manual booking ask Siri to book a ride for you.
  2. If you want to make a video call to your friend then you need to do manual calling by your self , ask Siri to make a video call for your friend.

Use Siri kit if your application belongs to any of the below domain :

  1. Audio or video calling: If your application has a feature of audio or video call then you can use Siri kit.
  2. Messaging Application: If your application support messaging feature then you can use it.
  3. Payments: If your application having payment feature then you can use it.
  4. Search photos: Photo searching app can also integrate Siri kit and utilize Siri feature.
  5. Workout: This category application can integrate Siri.
  6. Ride Booking: Finally ride booking app can also use it.
  7. How Siri interact with your application service:

As we know that two different application run its own sandbox area and they can’t interact with each other in iOS. Apple provided a mechanism that is known as Extension by which one application can share data and interact with another application.

Siri will use two Extension to interact with application service these are :

  1. Intent Extension:  Using Intent Extension, Siri and application service will be making communication. It is mandatory to use intent extension otherwise you cant use Siri.
  2. Intent UI Extension :  Intent UI Extension comes into the picture if you want to show custom view for showing confirmation detail to the user. When you will create a custom Intent UI Extension will provide you a separate storyboard to design your view.

Where Siri plays its role: Siri process the user spoken request and convert it to an intent object on which someone can take action.

Lets start creating a demo application that will give you more understanding and clarity.

Let create a single view iOS application and then will add Siri capability on it: One very important thing is , we can add Siri capability in existing iOS project as well as any newly created project.

Lets  create an iOS application named FirstSiriDemoApp, once you done with this you will get all default classes. Select the root project group Select File > New > Target. it will show you group of available extension for Siri select Intent Extension.

Screen Shot 2016-07-29 at 11.26.59 AM When you will tap on next it will asked you to include Intent UI Extension or not. If you want to include, keep the checkbox checked. When you will click on finish, it will create two new target with the name you have provided for you intent extension.

Lets figure out what are the default classes and assets provide in your Intent Extension

Screen Shot 2016-07-29 at 12.33.09 PM

I have expanded the target of FirstSiriDemoAppExtension and you can see that one default handler Xcode provide for us where will do the coding and info.plist where you can configure what type of domain capability you want to provide through Siri. I am listing here the no of  domain Siri supported.

  1. Audio or video calling
  2. Messaging
  3. Payments
  4. Searching photos
  5. Workouts
  6. Ride booking
  7. Configure plist file associated with Intent Extension target.

Open the NSExtension and NSExtensionAttributes keys to view the IntentsSupported key.

In the IntentsSupported key, add items for each intent you want to support. The type of each item must be String, and the value must be the class name of the intent.

Screen Shot 2016-07-29 at 12.53.07 PM

If you see the above figure we are adding the support of sending message.

configure iOS application for Siri capability:

Screen Shot 2016-07-29 at 12.59.20 PM

Flow between Siri and extension handler: This is the heart of Siri interaction, so need to understand very carefully. Lets divide this into steps:

  1. When user speak anything to Siri, Siri create intent object that could be converted into actionable item on which we can take action.
  2. Intent Extension handler receive the intent object from Siri and perform three task.
  • Resolve: This is the first phase where handler resolve all the parameter inside intent object and verify that we have sufficient detail to move further.

Lets take an example sending message as parameter.

[code]func resolveContent(forSendMessage intent: INSendMessageIntent, with completion: (INStringResolutionResult) -> Swift.Void) {
if let text = intent.content where !text.isEmpty {
completion(INStringResolutionResult.success(with: text))
}
else {
completion(INStringResolutionResult.needsValue())
}
[/code]

Here handler resolving content of message which we are going to send. We can check that, the message you are going to send has body.

  • Confirm: In this phase handler provides detail description of requested content to Siri and Siri can show to the users.
  • Handle: If we reached at this stage it means all parameters are valid and we can take the action. Now handler can handle the intent.

Note: When you are running you application, it needs the provisioning profile with Siri enabled.

 

Conclusion: So the domain of services supported Siri can avoid manual booking of rides , doing phone calls and messaging. You can also use the power of Siri and feel smart experience.

Happy Coding 🙂

FOUND THIS USEFUL? SHARE IT

comments (2)

  1. Raza

    Hi,
    Thanks for sharing nice Article. Do you have any Example which use for Sending and Receiving Payments?

    How we can integrate Siri with Existing iOS (with Objective-c) App rather then the Newly created App?

    Thanks.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *