Sub topic 17
Exposing app actions to Siri with App Intents
This chapter assumes you can read basic Swift, have Xcode installed, and know what a Shortcut is. You do not need to know SwiftUI in depth. The earlier chapters on Siri and Shortcuts cover how users trigger actions. This chapter covers the other side: how an app declares those actions so the system can offer them.
The working model
A Shortcut or a Siri request can only run what the system knows about. For built-in apps, Apple supplies those actions. For your own app, you describe them with the App Intents framework.
An intent is a small Swift type that represents one action, such as "Add Task". It has three parts:
- A title that people see in the Shortcuts app and in Siri results.
- Zero or more typed parameters, which are the inputs the action needs.
- A perform method, which runs the action and returns a result.
At build time, Xcode reads your intent types and writes metadata into your app bundle. The system reads that metadata, so your actions show up in Shortcuts without the app running. When someone triggers the action, the system launches your app's code (often in the background) and calls perform().
Defining an intent
This intent adds a task by name.
import AppIntents
struct AddTaskIntent: AppIntent {
static let title: LocalizedStringResource = "Add Task"
@Parameter(title: "Task Name")
var name: String
func perform() async throws -> some IntentResult {
// TaskStore is a placeholder for your app's own storage layer.
TaskStore.shared.add(name)
return .result()
}
}Points to notice:
- The type conforms to
AppIntent. That conformance is what the build step looks for. titleis a static property. It is localizable, so useLocalizedStringResourcerather than a plainString.@Parametermarks an input. The system builds the input field in Shortcuts from the Swift type. If the parameter is required and missing, Siri asks the user for it.perform()isasyncand canthrow. A thrown error is shown to the user as a failure of the action.TaskStoreis not part of the framework. Replace it with your own storage code, or the example will not compile.
Returning useful results
.result() returns nothing. Shortcuts can chain actions, so returning a value lets later steps use it. You declare the return type in the opaque result type:
import AppIntents
struct CountTasksIntent: AppIntent {
static let title: LocalizedStringResource = "Count Tasks"
func perform() async throws -> some IntentResult & ReturnsValue<Int> {
let count = TaskStore.shared.count
return .result(value: count)
}
}Here TaskStore.shared.count is again your own code. A Shortcut can take the integer and pass it to a "Show Notification" action, for example.
Making the action speakable: App Shortcuts
An intent alone appears in the Shortcuts app, but a user must build a Shortcut around it before saying anything to Siri. An App Shortcut removes that step. You register phrases, and Siri can run the intent immediately after the app is installed.
import AppIntents
struct TaskShortcuts: AppShortcutsProvider {
static var appShortcuts: [AppShortcut] {
AppShortcut(
intent: AddTaskIntent(),
phrases: ["Add a task in \(.applicationName)"],
shortTitle: "Add Task",
systemImageName: "plus"
)
}
}Rules that cause most first-time problems:
- Every phrase must include
\(.applicationName). This is how Siri tells your app apart from others. - An app has one
AppShortcutsProvider. Put all App Shortcuts inside it. shortTitleandsystemImageNameare what appear in the Shortcuts app and Spotlight. The image name is an SF Symbols name.- Parameters on the intent that are not filled in by the phrase are requested by Siri in a follow-up question.
| Piece | Where users see it | Needs a provider? |
|---|---|---|
| `AppIntent` only | Shortcuts app action list | No |
| `AppShortcut` | Siri phrases, Spotlight, Shortcuts app | Yes |
Connecting back to the command line
Because intents surface as Shortcut actions, a Shortcut that uses your intent can be run from Terminal like any other:
# List the Shortcuts available to the current user
shortcuts list
# Run a Shortcut by name
shortcuts run "Daily Summary"You can also give a Shortcut input and capture its output. This is useful when testing an intent that takes text and returns a value:
# Pass a text file as input to a Shortcut and save its output
shortcuts run "Clean Up Text" --input-path ~/Documents/draft.txt --output-path ~/Documents/draft-clean.txtThe command line runs the same path Siri does, so it gives a repeatable way to test without speaking aloud.
Edge cases and design decisions
Keep `perform()` fast and self-contained
The system may run your intent in the background without showing your app's window. Do not assume a window, a selected item, or any state that only exists after the user has clicked around. Load what you need from your storage inside perform().
Choose parameter types deliberately
Basic types like String, Int, Bool and Date work with little effort. For choices from a fixed list, define an enum that conforms to AppEnum. For your own data objects, define an AppEntity so Shortcuts can show a picker. A free-text String for something that is really a choice leads to typos and failed lookups.
Name actions as verbs
Titles like "Add Task" and "Count Tasks" read naturally in a Shortcut. Avoid titles that repeat the app name, since the app name is already shown beside the action.
Phrases are not guaranteed to match
Siri matches phrases loosely, but a phrase that is very short or similar to another app's can fail or be ambiguous. Use a verb and a noun, and test several wordings. Add more than one phrase per shortcut if people may say it differently.
Errors need clear messages
If perform() throws, the user sees the error. Throw an error type with a readable description instead of letting a generic failure escape.
import AppIntents
enum TaskError: Error, CustomLocalizedStringResourceConvertible {
case emptyName
var localizedStringResource: LocalizedStringResource {
switch self {
case .emptyName:
return "The task name can't be empty."
}
}
}Throw TaskError.emptyName from perform() when the name parameter is empty, and Siri or Shortcuts will show that sentence.
Troubleshooting your intents
If an action does not appear or Siri does not respond, check these in order:
- Build and run the app once. Metadata is generated at build time, and the system needs to see the installed app.
- Confirm the intent type conforms to
AppIntentand sits in the app target, not only in a test target. - Confirm each phrase includes
\(.applicationName). - Confirm Siri is enabled in System Settings, as covered earlier in this tutorial.
- Open the Shortcuts app and check whether the action is listed. If it is listed but Siri ignores the phrase, the problem is the phrase. If it is missing, the problem is the intent or build.
Summary of the flow
- Write an
AppIntentwith a title, parameters andperform(). - Optionally return a value so Shortcuts can chain it.
- Register an
AppShortcutsProviderwith phrases that include the app name. - Build and run, then test in Shortcuts, by Siri, or with
shortcuts run.
Exercise
You added an AppIntent to your app and it appears in the Shortcuts app, but saying "Add a task" to Siri does nothing. What is the most likely cause, and what is the fix?
Show answer
The intent has no App Shortcut registered, or its phrase does not include the app name. An intent alone only appears as a Shortcuts action. Add an AppShortcutsProvider with an AppShortcut whose phrases include \(.applicationName), such as "Add a task in \(.applicationName)", then rebuild and run the app.
Exercise
A Shortcut named "Count My Tasks" uses your CountTasksIntent. Write the Terminal command that runs it and saves the result to ~/count.txt.
Show answer
shortcuts run "Count My Tasks" --output-path ~/count.txt