How can I make my app provide shortcuts

Asked on 07/30/2024

1 search

To make your app provide shortcuts, you can use the App Intents framework. Here are the steps and key points to consider:

  1. Define Intents: Intents are actions that your app can perform, like opening a view or starting a specific task. They are the commands or verbs in your app.

  2. Create App Shortcuts: An app shortcut is a wrapper around an intent that highlights it as an important function of your app. This allows features like Spotlight and Siri to offer that function without needing your app to be running. You can pre-fill some or all of the parameters of the intent to make the shortcut more specific.

  3. Automatic Registration: The App Intents framework automatically detects the provider and handles registration, so your app shortcuts are available as soon as the app is installed. This means you don't need to write any registration code.

  4. Integration with System Features: App shortcuts can be used in various system features such as Spotlight, Siri, the action button, and Apple Pencil Pro. They also appear as actions in the Shortcuts app, where users can explore, combine, and remix them to create custom shortcuts.

  5. Code Example: Here is a brief code example to illustrate how to define an app shortcut:

    struct OpenPinnedTrailIntent: AppIntent {
        static var title: LocalizedStringResource = "Open Pinned Trail"
        
        func perform() async throws -> some IntentResult {
            // Your code to open the pinned trail
        }
    }
    
    struct MyAppShortcutsProvider: AppShortcutsProvider {
        static var appShortcuts: [AppShortcut] {
            return [
                AppShortcut(intent: OpenPinnedTrailIntent(), phrases: ["Open pinned trail in MyApp"])
            ]
        }
    }
    

    In this example, OpenPinnedTrailIntent is an intent that opens a pinned trail, and MyAppShortcutsProvider provides this intent as an app shortcut.

For more detailed guidance, you can refer to the session Bring your app’s core features to users with App Intents.

Relevant Sessions