How to use AppIntents in iOS 16

Issue #930

AppIntents

Declare AppShortcutsProvider, note that appShortcuts uses @AppShortcutsBuilder syntax

swift
import AppIntents

struct OurShortcutsProvider: AppShortcutsProvider {
    static var shortcutTileColor: ShortcutTileColor = .lightBlue

    @AppShortcutsBuilder
    static var appShortcuts: [AppShortcut] {
        AppShortcut(intent: AskIntent(), phrases: [
            "Hey Joy",
            "Ask \(.applicationName)"
        ])
    }
}

We can create an app intent in code

swift
import AppIntents
import OpenAI

struct AskIntent: AppIntent {
    static var title: LocalizedStringResource = "Hey Joy"
    static var description: IntentDescription = "Ask me anything"
    
    @Parameter(title: "Prompt")
    var prompt: String?
    
    static var parameterSummary: some ParameterSummary {
        Summary("Ask question \(\.$prompt)")
    }
    
    func perform() async throws -> some ProvidesDialog {
        guard let prompt else {
            throw $prompt.needsValueError("Please provide a question")
        }

        let client = ChatService()
        let content = try await client.ask(prompt: prompt)
        
        if let content {
            return .result(dialog: IntentDialog(stringLiteral: content))
        } else {
            return .result(dialog: "No valid answer")
        }
    }
}

Update parameters

If the intents have dynamic parameters, we can tell AppIntents to refresh its parameters list, using updateAppShortcutParameters

swift
OurShortcutsProvider.updateAppShortcutParameters()

Show tip view

Use SiriTipView

swift
SiriTipView(intent: ReorderIntent(), isVisible: $isVisible)
    .siriTipViewStyle(.black)

Use ShortcutsLink

swift
ShortcutsLink()
     .shortcutsLinkStyle(.whiteOutline)

Troubleshoot

Look for appintentsmetadataprocessor in build log for potential errors

Some possible warnings

Metadata extraction skipped. No AppIntents.framework dependency found.

Written by

I’m open source contributor, writer, speaker and product maker.

Start the conversation