App Intents with Siri

Sep 19 2024 · Swift 6.0, iOS 18, Xcode 16

Lesson 02: Siri Interactions with App Intents

Demo

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Open the starter project for this lesson. You’ll see some additions to the final project from Lesson 1 to get started here. These are similar to the intent introduced in the last lesson, so take a quick look before moving on.

Adding Data to Spotlight

As seen in the last lesson, the entities were added to the Spotlight index so they could be searched. This code was placed into the AppMain.swift file.

Task {
  try await CSSearchableIndex
    .default()
    .indexAppEntities(sessionDataManager.sessions.map(SessionEntity.init(session:)))
}

Making and Registering Shortcuts

Shortcuts can be created as another way to use the information that Spotlight indexed. This also provides the first hooks for Siri to get involved.

import Foundation
import AppIntents

class SessionShortcuts: AppShortcutsProvider {
  static var shortcutTileColor = ShortcutTileColor.navy
  static var appShortcuts: [AppShortcut] {
    AppShortcut(
      intent: GetSessionDetails(),
      phrases: [
        //"Get \(\.$sessionToGet) details in \(.applicationName)",
        //"Get details for \(\.$sessionToGet) in \(.applicationName)"
        "Get details in \(.applicationName)"
      ],
      shortTitle: "Get Details",
      systemImageName: "cloud.rainbow.half",
      parameterPresentation: ParameterPresentation(
        for: \.$sessionToGet,
        summary: Summary("Get \(\.$sessionToGet) details"),
        optionsCollections: {
          OptionsCollection(SessionEntityQuery(), title: "Favorite Sessions", systemImageName: "cloud.rainbow.half")
        }
      ))
    AppShortcut(
      intent: OpenFavorites(),
      phrases: [
        "Open Favorites in \(.applicationName)",
        "Show my favorite \(.applicationName)"
      ],
      shortTitle: "Open Favorites",
      systemImageName: "star.circle")
  }
}
SessionShortcuts.updateAppShortcutParameters()

Automatic Siri Support

Thanks to the phrases added to the shortcuts, Siri can now respond when the user utters those phrases. To see your spoken words on the screen while debugging, go to Settings -> Apple Intelligence & Siri -> Siri Responses and choose “Always Show Request.”

App Intent Domains

To introduce app intent domains into the Session Tracker app, you need an appropriate entity and intent to add macros to your code. Early in the Assistant Schema API betas, a limited set of domains, including browser, mail and photo, were available.

import AppIntents

struct OpenURLInTabIntent: AppIntent {
  static let title: LocalizedStringResource = "Open Session in Tab"

  @Parameter(title: "Session")
  var session: SessionEntity?

  func perform() async throws -> some ReturnsValue<SessionEntity?> {
    return .result()
  }

  static var parameterSummary: some ParameterSummary {
    Summary("Open \(\.$session) in a browser")
  }
}
@AssistantIntent(schema: .browser.createTab)
struct OpenURLInTabIntent: AppIntent {
@AssistantIntent(schema: .browser.createTab)
struct OpenURLInTabIntent: AppIntent {
  var url: URL?
  var isPrivate: Bool
  func perform() async throws -> some ReturnsValue<SessionEntity?> {
    return .result(value: session)
  }
@AssistantEntity(schema: .browser.tab)
struct SessionEntity: AppEntity, IndexedEntity {
  var url: URL?

  var isPrivate: Bool
  init(session: Session) {
    self.id = session.id
    self.imageName = session.featuredImage
    self.name = session.name
    self.sessionDescription = session.sessionDescription
    self.sessionLength = session.sessionLength
    self.url = session.url
    self.isPrivate = false
  }
See forum comments
Cinema mode Download course materials from Github
Previous: Instruction Next: Conclusion