Contact Us

Implement Siri feature in your application to Send Message without opening your application.

Click To Download Full Source Code

  • iOS: 10
  • Language: Swift
  • Xcode Version: 8
  • Mac OS: 10.12

Step 1: Create Simple Xcode Project

For this demo, first create a Xcode project by selecting ‘Single View Application’ type.

Step 2: Add Siri Extension

  • Now we need to add ‘Intents Extension’ in application. For that open this screen firstsiri_screen_1
  • Select ‘Intents Extension’ in your application and click on ‘Next’ button siri_screen_2
  • Enter name of the extension. Here I have entered ‘MessageIntent’. And then click on ‘Finish’ button.siri_screen_3
  • Once you click on ‘Finish’ button, user can see the below screen siri_screen_4.
  • In this screen Xcode is asking you to active scheme for this new extension. So you can use this scheme for building and debugging. Schemes can be chosen in the toolbar or Product menu. Please active scheme for ‘MessageIntent’.
  • Once you activate scheme for ‘MessageIntent’ scheme then Xcode will display the below screen.siri_screen_5
  • In this screen Xcode is asking you to activate scheme for ‘MessageIntentUI’ extension. So please activate scheme for this extension too.
  • By using ’MessageIntentUI’ extension you can display UI while doing operation with Siri. We will come to this later.
  • Now you are done with adding Siri extension.

Step 3: Understanding what files you are getting after adding Siri extension

In below screen shot siri_screen, you can see that there are two different extensions that were added.

  • MessageIntent: This extension handles all operations for Siri feature
  • MessageIntentUI: This extension is user interface for handling Siri operation.

MessageIntent:
In this extension, you will find that there are total two files:

  1. info.plist
  2. IntentHandler.swift

Info.plist:

  • In plist file you have to mention what type of operation you want to perform by using siri. siri_screen
  • Right now let us consider three extensions for this demo,
    INSendMessageIntent – To send message
    INSearchForMessagesIntent – Search new incoming message
    INSetMessageAttributeIntent – To set message content attribute
  • If your application has some different requirement like: search hotel, search taxi etc.,then you can select any intent to provide support. siri_screen

IntentHandler.swift:

  • In swift file you have to implement all operations which you like to handle as per supported intent type for Siri.
  • You can check all necessary and optional method from Siri intent class. Here in below screen shots you can find structure and methods for ‘INSendMessageIntent’.
    siri_screen
    siri_screen
    siri_screen

Step 4: Run on your iOS Device

  • Now connect your device with your system.
  • Select ’SiriDemo’ from the scheme and run. Once it runs on your device then stop it. siri_screen
  • Now select ‘MessageIntent’ from the scheme and run. siri_screen
  • Now select your application from this window to continue. In this case, application name is ’SiriDemo’ so select it to continue. siri_screen
  • Now stop operation again from Xcode.
  • Now enable Siri supports for your app manually [Settings -> Siri -> App Supports]
    siri_screen
    siri_screen
  • Now you are ready for Siri operation.

Step 5: How to test

  • Try the Siri command. Activate Siri either by long pressing the Home button, or by saying “Hey Siri!” (note: the “Hey Siri!” feature must be enabled in the settings first)
  • Try out some of the command “Send message using SiriDemo”.
    siri_screen
    siri_screen
  • Now Siri will display UI by using ‘MessageIntentUI’ extension. siri_screen
  • Now just speak contact name of the person to whom you want to send message and the message content.
    siri_screen
  • Once you speak the message, then Siri will ask you if it should proceed to send message. If you say ‘Yes’ then Siri will send message and if you say ‘No’ then Siri will not send message until you ask Siri to do it.
    siri_screen
    siri_screen
    siri_screen
  • So in this way Siri will handle your command to send a message to particular contact.

Step 6: How to handle Siri command inside Application

First of all you have to implement all necessary methods under ‘IntentHandler.swift’ class which are related all intent which you select.
Method 1: resolveRecipients
siri_screen

  • This is an optional method
  • In this method you will receive contact detail which user speaks.
  • So first you have to check if this contact is there in your application records.
  • If no records are found by the application then you have to call below block

  • If records are found by application, then you have to check if the matching contact count is equal to 1. Then you need to call below block.

  • If more than one match found, then you need to ask the user which option he/she needs to select. For that you need to call below block

  • If user has not provided any recipients then you have to call below block to ask user to speak contact name again.

Method 2: resolveContent
siri_screen

  • This is an optional method
  • In this method you will receive text content for the message.
  • If this method receives empty text content then you have to call below block to ask user to speak content again
  • If this method receives proper text then you have to call below block to proceed to send text message.

Method 3: confirm
siri_screen

  • This is an optional method
  • In this method you have verify all necessary condition to send message.
  • In this method you will have all details including message content and recipient.
  • If all validations are proper to proceed to send message then you need to call below block,
  • If validations are not proper then you need to call below block to terminate process,

Method 4: handle
siri_screen

  • This method is mandatory.
  • In this method you have to implement your logic to call API to send message to particular user.
  • And based on API call response you have to call block to indicate user on Siri screen for message status.
  • If message successfully sent, then you have to call below block,
  • If message is not getting sent, then you have to call below block,
  • You can set any type to handle failure state. You can find all types of failure stats in below screen shot. Please use any as per your requirement [Screen_16]

Click To Download Full Source Code

Ravi Bokade

Ravi Bokade | Sr. iOS Developer

I am a Sr. iOS developer at Yudiz Solutions Pvt. Ltd. - one of the leading Mobile App development companies. I am passionate both about making apps and writing on a diverse range of development topics.

Leave a Reply

Top
Yudiz Solutions Pvt. Ltd.
Career
Are you looking for a company where you are appreciated for your work?
Get a Free Quote
Tell Us Your Project Requirements

Career
Are you looking for a company where you are appreciated for your work?
Get a Free Quote
Tell Us Your Project Requirements