Adding support for shortcuts is a great way to differentiate your app and provide better integration into the iOS ecosystem. Shortcuts allow users to interact with your app without having to launch and navigate to a specific screen to perform a task. Shortcuts let you use voice, text, or spotlight to accomplish quick tasks without much mental overhead. Users can also use shortcuts to build large workflows and perform many tasks simultaneously.
Previously, adding shortcuts and publishing them to the system could be tedious and time-consuming. Once the shortcut was configured and available, we needed to figure out the best way to notify users of the shortcut’s existence and show them how to use it. With iOS 16, Apple has revamped the process for adding and publishing app shortcuts to iOS. The old process of adding shortcuts required a clunky visual editor and mapping code files to intent definition files.
The new App Intents framework lets you create shortcuts from the same language you use every day: Swift. Everything is statically typed and brought into iOS at install time. Shortcuts will be immediately available to users through Siri, the Shortcuts app, and Spotlight.
Now let’s take a closer look at this new easy way to add shortcuts to your system.
Start
Click to download the starter project. Material download Buttons at the top or bottom of the tutorial.
of break logger The app allows you to record your break times throughout the day. Open your starter app project. Build and run BreakLogger. An empty break list appears.
Tap. addition Click the button at the bottom of the screen and you’ll be prompted to record your break.
Select the break you want to add. Next, you will see a list of the breaks you have added. This includes the break we recorded earlier.
Currently, the only way to record breaks is from within the app. However, iOS has several other ways to interact with apps across its ecosystem. A user may want to record breaks as part of a larger workflow, combining multiple actions into her single shortcut. You may also want to tell Siri to record your breaks while you’re out and about without opening the app. Integrating the App Intents framework into your app enables these use cases.
iOS 16 introduced the App Intents framework. Shortcut definitions are now built entirely in Swift, with no code generation or additional steps required to make them available across iOS. You no longer need to use an intent definition file or visual editor. Use the first app intent to start building your own app intents.
Define your first app intent
Inside the starter project, sauce group and select new group.name the group intent. Right-click again within the new group and select. New file… .choose swift file Please select a template Next.name the file LogBreakIntent.swift.
Create a LogBreakIntent
structure that conforms to AppIntent
:
import AppIntents
import SwiftUI
// 1
struct LogBreakIntent: AppIntent
// 2
static let title: LocalizedStringResource = "Log a Break"
// 3
func perform() async throws -> some IntentResult & ProvidesDialog
// 4
let loggerManager = LoggerManager()
loggerManager.logBreak(for: .quarterHour)
// 5
return .result(dialog: "Logged a 15 minute break")
Here’s what’s happening in the code above.
- Create a
struct
Represents a shortcut. Add conformance to.AppIntent
protocol. - add
title
property.this is the typeLocalizedStringResource
This makes the string available for localization searches out of process. Note that this code will run even when the app is not in memory. - add
perform()
Ability to finally confirm compliance withAppIntent
protocol.indicates to returnIntentResult
And that it provides dialogue. - Use the.
LoggerManager
A convenient type that allows you to record 15-minute breaks. This action saves the break to the Core Data store. - returns
IntentResult
As a dialogue.
The current configuration allows users to create shortcuts, and intents from BreakLogger appear as available actions. These actions can be performed alone or as part of a larger shortcut made up of many actions. As soon as the user installs her BreakLogger, the intent will be available as a shortcut action.
Build and run to confirm that the updated code is installed in the simulator.Then put the app in the background using a keyboard shortcut Command + Shift + H
. The Shortcuts app will appear on the same screen. Open the shortcut.
Tap. + button on the top right.of new shortcut View load.
in new shortcut view, tap Add action. In the top segmented control, app.
select break logger from list Record your breaks.
Tap end.The view is closed and the app’s[ショートカット]Return to tab. A new shortcut will appear on your screen. The first app intent created a shortcut.
Tap the “Record Break” shortcut to run it. After a second or two, a dialog dropdown appears with your settings. perform()
function.
Tap end, close the shortcut and reopen BreakLogger. You will see that the break you just recorded has been added to the list.
well done! I added shortcut integration to my app in a few steps using some Swift files. Next, add the app shortcut to BreakLogger.
Add an app shortcut
Return to Xcode and add your new Swift file to the Intents group you created earlier.name the file BreakLoggerShortcuts.swift.
Add the following:
import AppIntents
// 1
struct BreakLoggerShortcuts: AppShortcutsProvider
// 2
static var appShortcuts: [AppShortcut]
// 3
AppShortcut(
// 4
intent: LogBreakIntent(),
// 5
phrases: [
"Log a break",
"Log a (.applicationName) break"
]
)
Here’s what this code does:
- creates a struct that conforms to
AppShortcutsProvider
. This tells iOS to index your shortcuts here. - Add a static property that returns an array of .
AppShortcut
instance. In this example, we will only provide one. - Create a
AppShortcut
. - Purpose
LogBreakIntent
as the intention behind the shortcut. - Provides a list of phrases that can trigger this shortcut.Be careful using special tokens
.applicationName
which resolves to the app’s localized name.
Then build and run.
Start the app in the background. If you launch Siri in the simulator and say “use her BreakLogger for a break,” the shortcut will run immediately.
You’ll be prompted to enable the BreakLogger shortcut in Siri.
Next, a confirmation dialog will appear.
Finally, open BreakLogger again. The second recorded break from the shortcut you ran is displayed.
Note: Siri can be particular about simulators. You may need to reset your device and try several times to be able to run shortcuts by voice. Also, make sure that the room you are working in does not have a lot of ambient noise.
Next, create a custom confirmation view to display each time the intent is executed.