Adding Shortcuts and Spotlight actions to iOS
Let's say, I want a “Shuffle Liked Songs” action that:
- shows in Spotlight
- shows in the Shortcuts app
- opens the app and runs the action
Implementation: App Intents
Define the intent:
struct ShuffleLikesIntent: AppIntent {
static let title: LocalizedStringResource = "Shuffle Liked Songs"
static let description = IntentDescription("Your favorite songs deserve another spin")
static let openAppWhenRun: Bool = true
@MainActor
func perform() async throws -> some IntentResult {
guard
let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
let delegate = scene.delegate as? SceneDelegate
else { return .result() }
delegate.performShuffleLikesAction()
return .result()
}
}
func performShuffleLikesAction() {
...
}
Notes:
openAppWhenRun = true, @MainActor if UI navigation required.
Register the shortcut:
struct ShortcutsProvider: AppShortcutsProvider {
static var appShortcuts: [AppShortcut] {
AppShortcut(
intent: ShuffleLikesIntent(),
phrases: [
"Shuffle my likes in \(.applicationName)",
"Play my likes in \(.applicationName)"
],
shortTitle: "Shuffle Liked Songs",
systemImageName: "shuffle"
)
}
}
Reminder
If you define “action” in both NSUserActivity with isEligibleForPrediction and App Intents, Shortcuts action can show duplicates.