Overview
In this blog, we will cover all the initial steps and theory that we should be aware and know about. Before starting to create any Siri shortcuts there are some points we are going to discuss today.
Minimum Requirement:
- Xcode 10 (beta or above)
- iOS 12 (beta or above)
We will create a project of the name “DemoSiriShortcut”. In it, we will perform “Siri shortcuts” for sending and requesting money. It will be simple, no API or real process is held only by using UserDefaults. Just to understand Siri Shortcut, we will add or subtract the value of UserDefaults.
Note: We are not integrating Siri; we are going to create the shortcut of the process, that we are going to access through Siri by adding a key-word to say. If you want to integrate siri in the app, you can go through another blog.
What is Siri Shortcut?
Siri shortcut is a shortcut of the process that we perform regularly in the app. It helps us to do the same by just saying a keyword that we have provided while adding that particular process to the Settings > Siri Search or Shortcuts app. Process can be performed directly or with the confirmation, in the background or with opening the app. It will also give a suggestion in the search, and spotlight and apple watch face too. You can access these shortcuts through iPhone, Apple watch, HomePod, and CarPlay too.
Available Siri shortcut domains
These are the types of activities(domains of intents) developer can use in the app:
- VoIP calling ?
- Messaging ?
- Notes and list ?
- Workout ??♂️
- Payment ?
- Visual Codes ?
- Photos ?
- Ride Booking ?
- Car command ?
- Car play ?
- Restaurant reservation ?
All the above are the domains that has inbuilt methods and handler that can be used directly in the project with Siri integration. And if you want to create other than above domain or its inbuilt methods, you can use “Custom Intents” which need to define all the parameters, actions and responses we want to use in-app. As we are going to use in this project.
Creating Certificate to access Sirikit in app
As we are using Siri feature in the app, we need to create the certificate. It’s quite simple to create the certificate. We should have our registered email id to use Siri. We need app bundle id and CSR to create the certificate. In this certificate, we have to enable Siri and app group capability of the app. You can create certificate in the Apple Developer Link.
After creating certificate, we need to add to the project in the Target > General > Signing(debug) and Signing(release) > Provisional profile > import profile. And selecting downloaded certificates through your id.
Then Build(⌘ + B) your project.
Now go to the capability section and turn on Siri and app group and add app group id as “group.(bundle identifier)” in the certificate.
Now you are ready to use Siri-shortcut in the project.
Sirikit(Intents Extension)
Intents help you to pass data from Siri to your app, even in the background, without opening the application. And allow you to do the activity that is related to that specific domain that you have provided. Like for the payment, it helps you to send or receive amount to the phone that you had said to Siri.
Payment has a protocol like INRequestPaymentIntentHandling, which helps you to handle all the parameter you need to use to request the payment. It also helps you to create UI that you need to show in the Siri. All the possible case are covered in this protocol, still if you want to do some custom process you can use “Intent Definition File” as we are going to use.
For the Intent Extension file,
Files > New > Target > iOS(tab) > Application Extension (section) > Intent Extension
Then, Next > (Give file name) > Finish
Note:- Make sure the data that you want to use in the Siri and app should have only one path like one API or UserDefaults so that both the data don’t have the different value or obj. And it should be stored somewhere. No simple variable will work, as when you open your app every variable will be initialized again.
After creating it you will find the folder with the name you have given. IntentHander and Plist file will be in it. Now, Go to the target and check if both the target’s capabilities section has activated “App Groups” and have the same id as added to the certificate’s app group id.
Now you will get a .entitlement file in both the target. Check if there is any id inside the App group list. (if not then add app group id)
We will come to IntentHandler and Plist file after next step.
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
- Title: Heading of the process
- Description: 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.
- 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
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 } }
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) } } } }
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:
- WithdrawDefinitionIntentHandling : It is the custom protocol that is created when we have added new intent in the intentDefinition file. So extend it and use it’s handle and confirmation method.
- WithdrawDefinitionIntentResponse : It is type of response for withdraw intent.
- WithdrawDefinitionIntentResponseCode.failDueToLessAmount : It is type of the response have added in the response of intentDefinition file.
- completion(response) : When all the handling is done then we give the response in this block. It can be success or failure or can be custom that you have added.
Donating shortcut
It passes the data to Siri that we have provided to create the shortcut. All this detail should be filled such that the user can easily use. It does everything inline and in the background, according to the task, we have donated. You can add button for the user to add shortcut or add it when the process is executed successfully.
// 1. Intent variable let intent = DepositeDefinitionIntent() intent.amount = NSNumber(value: amount) // amount is entered by the user in the textField //2. Interaction variable let interaction = INInteraction(intent: intent, response: nil) // 3. Donating interaction interaction.donate { (error) in guard error == nil else { print("Request problem : \(String(describing: error?.localizedDescription))") return } // If no error then your shortcut has been successfully added to shortcut print("Request Intent Donated") }
Run the app. And your Siri shortcut app is ready to use.
Hint: If you want to make ur shortcut more reliable, make siri suggest it and view in the search list and in the lock screen, you can add NSUserActivity in it.