Sub topic 17
Exposing app actions to Siri with App Intents
This chapter assumes you can read basic Swift, have Xcode installed, and know how to run a Shortcut from Siri (covered in the earlier Shortcuts chapters). It is aimed at developers. Other readers can skim it to see how apps appear in Shortcuts.
What App Intents do
An App Intent is a Swift type that describes one action your app can perform. The system reads these types and offers each action in three places: the Shortcuts app, Siri, and Spotlight. You write the action once, and the system handles the interface around it.
Step 1: Define the intent
An intent needs a title, any typed parameters, and a perform() method. The TaskStore type below is a minimal stand-in so the example compiles. Replace it with your own storage.
import AppIntents
final class TaskStore {
static let shared = TaskStore()
private(set) var tasks: [String] = []
func add(_ name: String) { tasks.append(name) }
}
struct AddTaskIntent: AppIntent {
static let title: LocalizedStringResource = "Add Task"
@Parameter(title: "Task Name")
var name: String
func perform() async throws -> some IntentResult {
TaskStore.shared.add(name)
return .result()
}
}The @Parameter property tells the system that this action needs a text value. Siri asks the user for it if it is missing, and Shortcuts shows it as an input field.
Step 2: Register spoken phrases
An intent alone appears in the Shortcuts app. To let people say it to Siri without building a Shortcut first, add an AppShortcutsProvider.
import AppIntents
struct TaskShortcuts: AppShortcutsProvider {
static var appShortcuts: [AppShortcut] {
AppShortcut(
intent: AddTaskIntent(),
phrases: ["Add a task in \(.applicationName)"],
shortTitle: "Add Task",
systemImageName: "plus"
)
}
}Every phrase must contain \(.applicationName). This lets Siri tell which app you mean.
Common errors
| Symptom | Likely cause |
|---|---|
| Phrase is ignored | The phrase does not include the app name |
| Intent missing from Shortcuts | The app has not been built and run since the intent was added |
| Siri asks for a value every time | The parameter is required and the phrase does not supply it |
| Action does nothing | `perform()` returns without doing the work, or the work runs on the wrong actor |
Checking your work
- Build and run the app on your Mac.
- Open the Shortcuts app and search for "Add Task". Your intent should be listed under your app.
- Say or type "Add a task in" followed by your app name to Siri. Siri should ask for the task name.
- Run
shortcuts listin Terminal. Shortcuts you build from the intent appear there and run withshortcuts run.
Exercise
Why must each phrase in an AppShortcut include \(.applicationName)?
Show answer
The system uses the app name in the phrase to match what the user says to the correct app, so Siri can run the intent without any Shortcut setup. Phrases without it are not accepted.
Exercise
Which two parts of AddTaskIntent decide what Siri asks the user and what the app does?
Show answer
The @Parameter property decides what Siri asks for, because it declares a typed input. The perform() method decides what the app does with that input.