Skip to content
Khoa Pham@onmyway133

How to use AppIntents in iOS 16

1 min read

AppIntents

Declare AppShortcutsProvider, note that appShortcuts uses @AppShortcutsBuilder syntax

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

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

OurShortcutsProvider.updateAppShortcutParameters()

Show tip view

Use SiriTipView

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

Use ShortcutsLink

ShortcutsLink()
     .shortcutsLinkStyle(.whiteOutline)

Troubleshoot

Look for appintentsmetadataprocessor in build log for potential errors

Some possible warnings

Metadata extraction skipped. No AppIntents.framework dependency found.

Keep reading

Type to search titles and full text.

↑ ↓ move↵ openPagefind