Using App Intents in Swift/SwiftUI

Troy Husted - Aug 24 - - Dev Community

When I finished the core functionality of my app Recitation, I wanted to include features that would not only increase the accessibility but also add more customization.

App Intents definitely intersect those two - allowing users to add shortcuts, interact with my app using Siri, and with iOS 18 coming I can expose features of my app to Apple Intelligence.

Here is an example of adding app intents to your app, it's super simple.

Import AppIntents & Code Skeleton

To create your first app intent, all you need is a simple skeleton function that you can copy/paste and fill out for your own needs. Here is an example from my app, where you can create a quick shortcut to open the "Create Task" view.

struct CreateTaskIntent: AppIntent {

    static let title: LocalizedStringResource = "Create Task"
    static let description: LocalizedStringResource = "Opens task creation view"
    static let openAppWhenRun: Bool = true

    @MainActor
    func perform() async throws -> some IntentResult {
        if let url = URL(string: "recitation://create-task") {
            await UIApplication.shared.open(url)
        }
        return .result()
    }
}
Enter fullscreen mode Exit fullscreen mode

Here, we can see a few different key elements. First off, each app intent has a title, description, and a boolean to indicate if the app should be opened or not.

Then, in the MainActor (MainActor is necessary here to ensure all updates are happening on the main thread) we actually perform the action. So, whether you're updating core data models, displaying some information to the user, or in my case opening a deep link, it all has to happen in the perform() method.

Finally, we return a result. In this example, the result is empty because we don't have anything to tell the user - we simply open the create task view. However, you have the option to return information such as a value and dialog.

Exposing this Intent to the System

Now that we have our intent made, we need to let the system know that this actually exists.

This is actually incredibly easy - we'll create a new file and include the following.

struct TransferAppShortcutsProvider: AppShortcutsProvider {
    static var appShortcuts: [AppShortcut] {
        AppShortcut(
            intent: CreateTaskIntent(),
            phrases: [
                "Create a \(.applicationName) task",
                "Create a task in \(.applicationName)",
                "Add task in \(.applicationName)",
                "Add a task in \(.applicationName)"
            ],
            shortTitle: "New Task",
            systemImageName: "plus"
        )
   }
}
Enter fullscreen mode Exit fullscreen mode

In your AppShortcutsProvider, you can include a list of all your AppShortcuts. Using the above mentioned CreateTaskIntent example, all we have to do is include a title and image name that shortcuts can recognize. The phrases are optional and to my knowledge you can include as many or as little as you want, though they must include your application name.

Build and Run

Once you build and run, open up the shortcuts app and see if you can find your shortcut!

. .