Siri Shortcuts on Apple Watch
Learn how to take advantage of Siri and Shortcuts on the Apple Watch without any intervention required from the iOS companion app. By Mark Struzinski.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Siri Shortcuts on Apple Watch
30 mins
- Getting Started
- Running the App
- Setting up Portal Requirements
- Creating an App Group
- Configuring Your Account
- Understanding Siri Shortcuts and Intents
- Using NSUserActivity-Based Shortcuts
- Using Intents-Based Shortcuts
- Executing the App Remotely
- Adding an Intent-Based Shortcut
- Adding the Intent Definition File
- Configuring Types
- Configuring a New Intent
- Setting up Intent Parameters
- Explaining Parameters
- Understanding More Parameters
- Configuring the Shortcuts App
- Defining Suggestions
- Defining Siri’s Response
- Adding the Intents Extensions
- Adding Framework Dependencies
- Configuring for Builds
- Configuring Identifiers
- Enabling Entitlements Files
- Updating Info.plist Files
- Generating Intent Code
- Sharing Intent Handling
- Resolving Parameters
- Connecting the Handlers
- Testing Your Intent
- Testing With Voice
- Validating Input
- Adding Your App to the Watch Face
- Where to Go From Here?
In iOS 14 and watchOS 7, Apple extended Shortcuts to the Apple Watch. Shortcuts can now run directly on the watch. While the phone is still necessary to create shortcuts, the watch can run them independently.
In this tutorial, you’ll:
- Take advantage of shortcuts and Siri on the Apple Watch.
- Create shortcuts for your app that run independently on the watch.
- Enable Siri to function with your app through the watch, without any intervention required from the iOS companion app.
- Add a shortcut for your app on the watch face.
-
A paid Apple Developer account.
- This tutorial uses App Groups to share data between the apps and the intents you’ll build. Using App Groups requires some portal setup and a paid developer account.
-
A physical iPhone running iOS 14 and an Apple Watch running watchOS 7.
- Although it’s possible to test the watch app from the simulator, it’s impossible to build and test intents on the simulator. To see your intents in action, you need a physical watch.
-
A paid Apple Developer account.
- This tutorial uses App Groups to share data between the apps and the intents you’ll build. Using App Groups requires some portal setup and a paid developer account.
-
A physical iPhone running iOS 14 and an Apple Watch running watchOS 7.
- Although it’s possible to test the watch app from the simulator, it’s impossible to build and test intents on the simulator. To see your intents in action, you need a physical watch.
Please make sure you sign into Xcode with your developer account. This allows automatic provisioning to create all the necessary app identifiers and provisioning profiles as you work through this tutorial.
Getting Started
Download the starter project by clicking the Download Materials button at the top or bottom of the tutorial. Open the app in the starter directory. This app assists in tracking your hydration intake throughout the day. It runs on the phone and the watch. Before you check it out, you need to make sure you have your simulator set up with a paired Apple Watch.
If you don’t already have an iPhone simulator paired with a watch, follow these steps:
- In Xcode, select Window ▸ Devices and Simulators.
- Select Simulators on the top left.
- Click + on the bottom left to add a new simulator.
-
In the dialog, fill in the following information:
- Simulator Name: Make this something that describes the combination of phone and watch.
- Device Type: Select any device you like here.
- OS Version: iOS 14.3 or the latest SDK.
- Check Paired Apple Watch.
- Click Next.
-
In the second step, fill in the following information:
- Paired Simulator Name: Anything that accurately describes the watch simulator you want to use.
- Device Type: Select any device you like here.
- OS Version: watchOS 7.2 or the latest SDK.
- Click Create.
- Your newly created simulator pair will now appear in the Run Destination list:
Running the App
You can now run the app and access it from both the phone and watch simulators. First, check out the iOS app.
- Select Hydrator from the scheme menu and select your paired phone and watch simulator in the Run Destination menu.
- Build and run.
- Add a few hydration entries by tapping Create New Entry.
- Check out the list of entries on the main view.
Next, check out the app on the watch.
- Switch the active scheme to HydratorWatch.
- Build and run again.
- Tap + and add some hydration entries to see them populate in the list.
The app uses frameworks and app groups to share some code between the iOS app, watch app and extensions. This work is complete already. You’re just pulling in the frameworks when necessary to read and save data.
Setting up Portal Requirements
The starter project uses .xcconfig files to propagate build settings across all targets.
Creating an App Group
Before you configure your project, you need to create an App Group.
- Log in to the developer portal using your account.
- Select Certificates, IDs & Profiles from the sidebar.
- Now, choose Identifiers and press the blue + button to create a new identifier.
- From the list of options available, select App Groups.
- Fill in Description and Identifier. Note that the identifier will add group automatically at the beginning. Also, try to use reverse domain notation as recommended.
Configuring Your Account
Perform these setup steps to get going:
- In the Xcode Project navigator, find the Resources/config folder.
- Open base.xcconfig.
- Update the value of BASE_DOMAIN to include a unique string. Apple recommends reverse domain notation here, like com.yourcompanyname.
- Update the DEVELOPMENT_TEAM value, replacing YOUR-TEAM-ID-HERE with your Team ID from the developer portal. If you’re not sure what it is, log in to the Apple Developer portal. Click the Account tab and then select Membership from the left sidebar. Your Team ID is on this page.
- Update GROUP_ID, replacing YOUR-GROUP-ID-HERE with the value you already created in your Apple account.
- Save the .xcconfig file by pressing Command-S.
Understanding Siri Shortcuts and Intents
When you build your app for Siri Shortcuts, you have two options: NSUserActivity-based shortcuts and intents-based shortcuts. By far, the more flexible and robust option is to use intents-based shortcuts. They give you more options in accepting parameters, conflict resolution and a robust object model for input.
Using NSUserActivity-Based Shortcuts
Shortcuts that use the NSUserActivity API require an app launch to function. They’re unable to execute requests in the background with Siri. In your watch app, NSUserActivity-based shortcuts will attempt to open the watch app to execute first. If there’s no watch app present, the shortcut fails.
Using Intents-Based Shortcuts
Intents-based shortcuts can run locally via an extension, through the watch app or remotely on the phone. An intents-based shortcut that starts on the watch follows this flow:
- First, if it’s possible to handle the shortcut on the watch — because an intents extension is present on the watch — then the watch handles the shortcut locally.
- If no intent extension is present on the watch, then the watch attempts remote execution on the paired iPhone.
- Next, if the shortcut requires an app launch, then the shortcut fails. Shortcuts started from the watch cannot launch their iOS app counterpart to execute.
- Lastly, if the extension can run in the background on the iOS device, it’s executed there.