iOS 10: Integrating Siri Kit to your apps – Part 2

Audio : Listen to This Blog.

In the Part 1 of Siri Kit article, we have covered the basics of using SiriKit. From here we shall learn how to implement SiriKit in your own app. We have made a sample app MobiMessenger in best way to explain further section of this article. Please download here the full source code for the project. Here we’ll cover how it works and how to implement Siri into our app.
Now you must know the Siri structure. When you are giving a command to Siri, she will recognize and prepares an Intent. The Intent is an object containing the properties specific to the command.

Lets see some example from our app:

Above example, Siri will ask more question depends on your sentence formed and finally prepares the Intent as follows:
App Name: MobiMessenger  Domain: Messaging Intent: Send Message
Recipient : Celestra    Message:  I really excited to do this
See one more (ex:) : “ Hey Siri, send Buttercup 85 bucks on UnicornPay for rainbow polish” , here Siri recognise the sentence then choose the domain. And the Intent is:
App Name: UnicornPay Domain: Payments Intent: Send Payments
Payee : Buttercup    Amount:85

Siri Kit

How to implement Siri into our app ?
  1. Ensure you have Xcode 8 (now Beta) version. Download the sample code we have given for better understanding.
  2. Enable Siri for development in the target device where you want to develop and test the app. (please note that the Xcode Simulator cannot test the Siri)
  3. Siri Kit Entitlement for your app as below:
  4. Now create Intents Extension in Xcode ( File >  New > Target > Intents Extension )
  5. Once created, we will get two targets as Siri Kit & Siri KitUI
  6. In “Siri Kit” Intent extension contains a separate Info.plist file, now add the supported intents as below illustration :

Since our sample app belongs to ‘Messaging domains”, the IntentsSupported array should be filled with supported Intents as INSendMessageIntent, InStartWorkoutIntent.

  1. In the project, we have two targets generate as Siri Kit & Siri KitUI, here first one is for intent handler and second one for Custom UI handle where we can customize the Intent response  through MainInterface.storyboard.
  2. Open the IntentHandle.m file where we need t confirm to the protocol INSendMessageIntent, InStartWorkoutIntent.

@interface IntentHandler ( ) <INSendMessageIntentHandling, INStartWorkoutIntentHandling>
@end

  1. Now we need to implement protocol methods in the IntentHandle.m file.

– (void)resolveRecipientsForSendMessage:(INSendMessageIntent *)intent
– (void)resolveContentForSendMessage:(INSendMessageIntent *)intent
– (void)confirmSendMessage:(INSendMessageIntent *)intent
– (void)handleSendMessage:(INSendMessageIntent *)intent
To get more insight on importance of these protocol methods lets learn the Life cycle of intent.

  1. Life cycle of intent:

See below illustration:
Siri shall ask multiple questions to “Resolve” and finally derive a task based on the user speech. Once Siri recognizes your intent then “Confirm” will execute as below illustration:
Finally, we can send the message through the “Handle” steps.

How to Run the Siri Kit & Siri KitUI Target?

If you want to debug the Siri Kit intent ( intoIntentHandle {.h. m } ) then we need to choose the target file and run with you’re device.
Afterwards, it will now prompt asking which app to attach to, just select the app you just ran, MobiMessenger.
This will present the app again on your device (a white screen/blank app most likely), but this time the debugger will be attached to the Intent extension.

Conclusion

In this article we explored the SiriKit with a sample code. We hope this series on Siri helps to dive into SiriKit and make your own apps with Siri Enabled. We at Mobinius in the process to upgrade various iOS apps to enable capability of the Siri and hence provide users next best user experience.
For now, read on, have fun, implement, and apply Siri to your next great app idea!

Guest Author:

Tamil Selvan, iOS developer @ Mobinius

Read Similar Blogs