Siri Intents for WatchOS

Building custom intents for WatchOS apps: A step-by-step guide.

Faaiz D
Simform Engineering

--

In Swift, an intent is a brief overview of a task that a user wants to complete. It aims to record the user’s intent in a way that may be used by the app and is understandable to the user.

In this blog, we will walk you through a step-by-step process of building custom intents for your WatchOS app, using a delightful example: ordering coffee through your smartwatch. Imagine the convenience of simply raising your wrist and saying, “Hey Siri, order a coffee,” and having it ready from your given cafe. That’s the kind of user experience custom intents can deliver.

Table of contents

  1. Create a New Intent Definition File
  2. Define Your Intent Parameters and Actions
  3. Implement Your Custom Intent in Your WatchOS App
  4. Adding shortcut in iPhone
  5. Synchronizing iPhone Shortcuts with Your Apple Watch
  6. Handle intent with the help of user activity

Before we jump in…

To enable your WatchOS app to use custom intents with Siri, you’ll need to enable the SiriKit entitlement in your target’s signing and capabilities tab.

Add Siri in capability

Now that the initial setup has been done successfully, it’s time to begin the actual implementation.

1. Create a new intent definition file

You must first create a new intent definition file that defines the parameters and data types associated with the intent in order to build a custom intent. Siri will use this file to identify and handle user requests. It will define the conditions and actions of your custom intent.

To create a new intent definition file, follow these steps:

  • In Xcode, go to File -> New -> File.
  • Select “SiriKit Intent Definition” from the iOS section.
  • Choose a name and location for your file, and click “Create”.
New intent definition file creation

2. Define your intent parameters and actions

Intent actions are the tasks that your intent will carry out, intent parameters are the values that must be present in order for your intent to be carried out.

  • Open your intent definition file in Xcode.
  • Create an intent action by clicking the “+” button under your definition file.
  • Click the “+” button under “Intent Parameters” to add a new parameter.
  • Enter a name and description for your parameter, and select its type (e.g., number, date, time, etc.).

For example, let’s say we want to create a custom intent for ordering coffee from your favorite cafe. For that, we could define the parameter like this:

  • CafeName: Name of cafe you want to order from.
Define your intent parameters and actions

3. Implement your custom intent in your WatchOS app

You have to integrate your custom intent into your WatchOS app after you’ve defined it. To do this, create a class of intent handlers that will respond to user requests for your custom intent.

The steps below must be followed to implement your custom intent in your WatchOS app:

  • Go to File -> New -> Target in Xcode.
  • From the WatchOS platform, select “Intents Extension”.
  • Click “Create” after selecting a name and location for your extension.
  • Create a new Swift file dedicated to handling your custom intent. Be sure to select the watch target for this file.
  • Implement the “handle” and “resolve” methods in your intent handler class to be used when the user triggers your unique intent.
  • Use the user-provided parameters to execute the proper action within your app.

Here’s an example of how you can create a handling class for a custom intent. In this class, we extend the functionality of the automatically generated OrderIntentHandling class, which is generated when you create a new custom intent. This custom handling class is designed to help you efficiently manage the actions associated with your custom intent.

[Class for handling intent]

Now use this class in IntentHandler:

[Intent handlet class usage]

4. Adding a shortcut in iPhone

In the case of watch applications, the direct addition of shortcuts isn’t possible. Therefore, the process entails creating the shortcut within the iPhone app initially. Let’s proceed with the steps to accomplish this:

[Setup for adding shortcut in iPhone]
  • The INUIAddVoiceShortcutViewController is instantiated with the shortcut and the delegate is set to the coordinator.
  • If the user completes the setup, addVoiceShortcutViewController(_:didFinishWith:error:) is called, potentially saving the voice shortcut. If the user cancels, addVoiceShortcutViewControllerDidCancel(_:) is called.

Overall, the above code sets up an interface to add a Siri Shortcut for ordering coffee, providing a seamless integration of UIKit’s INUIAddVoiceShortcutViewController.

5. Synchronizing iPhone shortcuts with your Apple watch

To display shortcuts in the watch shortcuts app, you need to follow the below steps:

  • On your iPhone, locate the shortcut you want to display on your Apple Watch.
  • Long-press on the shortcut to bring up additional options.
  • Select “Details” from the options that appear.
  • Within the shortcut details, you’ll find a switch labeled “Show on Apple Watch.” Toggle this switch to the “On” position.
  • After a short delay, the shortcut will be visible and accessible in the Apple Watch Shortcuts app.

6. Handle intent with the help of user activity

You can handle your intent from handleUserActivity method in the app delegate for UIKit or with .onContinueUserActivity in SwiftUI. You will get your intent parameter here, and you can do your task from here.

For UIKit:

[Handling user activity in UIKit]

For SwiftUI:

[Handling user activity in SwiftUI]

Final Output

References

Conclusion

Crafting custom intents for WatchOS apps can be a powerful way to enhance user experiences and make your app stand out in the competitive world of wearable technology. By following the step-by-step guide outlined in this blog, you’ve learned how to create personalized, context-aware interactions that can simplify tasks, improve efficiency, and engage users on a deeper level. Whether you’re building a fitness app that tracks workouts, a productivity app for managing tasks, or an entertainment app for controlling media playback, custom intents can elevate your app’s functionality and user satisfaction.

For more updates on the latest tools and technologies, follow the Simform Engineering blog.

Follow Us: Twitter | LinkedIn

--

--