Custom Intent Definition & Response to that shortcut

We will add custom intents file as we want try custom method, but it is almost same only new file will be added which has all the information regarding what to get and pass to/from user.

In this file we have to provide each and every action that we are going to give shortcut. In this project I have 2 actions – Sending and Requesting money action. I will add 2 intents.

To add the file File > New > File >In the Resource section > Select – Siri Intent Definition File > (Give file name) > Create

Now on Left Bottom Side of the file you click + sign > New Intent

Give the name of the intent as particular process you are adding in the file.

As you can see in the above screenshot, there are certain words you need to understand, so here these are the definitions of them.

Custom Intent Category: Type of process you need to perform

Type of process you need to perform Title: Heading of the process

Heading of the process Description: Small description on the process

Small description on the process Confirmation: Whether this process need user’s confirmation or can be done directly Parameter Parameter: Variables(value) and its type you need from the user. It can be multiple. Provide all the variable in the section. Shortcut Types Parameter Combination: When you click + button it will ask for the combination of the parameters you need to use in same type of the process Title: Data you want to show in the Siri shortcut list title.You can use parameter in the title too Subtitle: Data you want to show in the Siri shortcut list subtitle. You can use parameter in the subtitle too Background: Is this process to be done in the background or you need to open the app.

When you click button it will ask for the combination of the parameters you need to use in same type of the process

Anyway you will see only Title and subtitle of shortcut parameter section everywhere. So make sure it helps the user to understand the shortcut, don’t confuse or mislead them.

Now, let’s come to IntentHandler and plist again.

First of all, let’s take plist file and add string as “(name of intents in intentDefinition file)” + “Intent” as postfix in the NSExtension > NSExtentionAttributes > IntentsSupports, that you have added in the intentDefinition file.

In the intentHandler file, initially your file should have only these codes.

Remove all other extra code if any.

Now, we will add all the handler methods that we have added in the intentDefiniton file.

// MARK: - Withdraw Intent Handling extension IntentHandler: WithdrawDefinitionIntentHandling { func handle(intent: WithdrawDefinitionIntent, completion: @escaping (WithdrawDefinitionIntentResponse) -> Void) { // definition here } func confirm(intent: WithdrawDefinitionIntent, completion: @escaping (WithdrawDefinitionIntentResponse) -> Void) { // definition here } } 1 2 3 4 5 6 7 8 9 // MARK: - Withdraw Intent Handling extension IntentHandler : WithdrawDefinitionIntentHandling { func handle ( intent : WithdrawDefinitionIntent , completion : @ escaping ( WithdrawDefinitionIntentResponse ) -> Void ) { // definition here } func confirm ( intent : WithdrawDefinitionIntent , completion : @ escaping ( WithdrawDefinitionIntentResponse ) -> Void ) { // definition here } }

We are going to use only handle method right now. Confirmation method can be used to check in this process is ready for the next step or not. So let us move forward and give proper definition for all the possible condition and response in the handling method.

Like I have done for the withdraw process.

// MARK: - Withdraw Intent Handling extension IntentHandler: WithdrawDefinitionIntentHandling { func handle(intent: WithdrawDefinitionIntent, completion: @escaping (WithdrawDefinitionIntentResponse) -> Void) { if let amount = intent.amount?.intValue {//1. Getting the amount to be withdraw if let newBalance = PaymentDetails.withdraw(amount: amount) {//2. Taking new balance by invoking the method to withdraw //3.Creating response let response = WithdrawDefinitionIntentResponse(code: WithdrawDefinitionIntentResponseCode.successWithAmount , userActivity: nil) //4. As we have to give new balance in the response response.availableBalance = NSNumber(value: newBalance) completion(response) } else { // If your balance is less then the amount to be withdraw then error let response = WithdrawDefinitionIntentResponse(code: WithdrawDefinitionIntentResponseCode.failDueToLessAmount , userActivity: nil) response.availableBalance = NSNumber(value: PaymentDetails.checkBalance()!) response.requestAmount = NSNumber(value: amount) completion(response) } } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 // MARK: - Withdraw Intent Handling extension IntentHandler : WithdrawDefinitionIntentHandling { func handle ( intent : WithdrawDefinitionIntent , completion : @ escaping ( WithdrawDefinitionIntentResponse ) -> Void ) { if let amount = intent . amount ? . intValue { //1. Getting the amount to be withdraw if let newBalance = PaymentDetails . withdraw ( amount : amount ) { //2. Taking new balance by invoking the method to withdraw //3.Creating response let response = WithdrawDefinitionIntentResponse ( code : WithdrawDefinitionIntentResponseCode . successWithAmount , userActivity : nil ) //4. As we have to give new balance in the response response . availableBalance = NSNumber ( value : newBalance ) completion ( response ) } else { // If your balance is less then the amount to be withdraw then error let response = WithdrawDefinitionIntentResponse ( code : WithdrawDefinitionIntentResponseCode . failDueToLessAmount , userActivity : nil ) response . availableBalance = NSNumber ( value : PaymentDetails . checkBalance ( ) ! ) response . requestAmount = NSNumber ( value : amount ) completion ( response ) } } } }

You will be thinking:

No..! Not at all. It is very simple and easy to understand. Let’s discuss all the important keywords used in the above code: