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 first
- Select ‘Intents Extension’ in your application and click on ‘Next’ button
- Enter name of the extension. Here I have entered ‘MessageIntent’. And then click on ‘Finish’ button.
- Once you click on ‘Finish’ button, user can see the below screen .
- 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.
- 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 , 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:
- info.plist
- IntentHandler.swift
Info.plist:
- In plist file you have to mention what type of operation you want to perform by using siri.
- 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.
IntentHandler.swift:
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.
- Now select ‘MessageIntent’ from the scheme and run.
- Now select your application from this window to continue. In this case, application name is ’SiriDemo’ so select it to continue.
- Now stop operation again from Xcode.
- Now enable Siri supports for your app manually [Settings -> Siri -> App Supports]
- 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”.
- Now Siri will display UI by using ‘MessageIntentUI’ extension.
- Now just speak contact name of the person to whom you want to send message and the message content.
- 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.
- 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
- 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
var resolutionResults = [INPersonResolutionResult]() resolutionResults += [INPersonResolutionResult.unsupported()] completion(resolutionResults)
- 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.
var resolutionResults = [INPersonResolutionResult]() resolutionResults += [INPersonResolutionResult.success(with: recipient)] completion(resolutionResults)
- 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
var resolutionResults = [INPersonResolutionResult]() resolutionResults += [INPersonResolutionResult.disambiguation(with: matchingContacts)] completion(resolutionResults)
- If user has not provided any recipients then you have to call below block to ask user to speak contact name again.
completion([INPersonResolutionResult.needsValue()])
- 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
completion(INStringResolutionResult.needsValue())
- If this method receives proper text then you have to call below block to proceed to send text message.
completion(INStringResolutionResult.success(with: text))
- 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,
completion(INStringResolutionResult.success(with: text))
- If validations are not proper then you need to call below block to terminate process,
completion(INStringResolutionResult.needsValue())
- 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,
let userActivity = NSUserActivity(activityType: NSStringFromClass(INSendMessageIntent.self)) let response = INSendMessageIntentResponse(code: .success, userActivity: userActivity) completion(response)
- If message is not getting sent, then you have to call below block,
let userActivity = NSUserActivity(activityType: NSStringFromClass(INSendMessageIntent.self)) let response = INSendMessageIntentResponse(code: .failure, userActivity: userActivity) completion(response)
- 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]