Instruction

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

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

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

Unlock now

So far, intents like OpenFavorites or OpenSessionIntent have caused the app to open to present data to the user. If you’ve ever used Siri with CarPlay or a HomePod, you know that visual feedback is either impossible or not desired. Also, opening the app you’re running an intent for will push you out of the current app you’re viewing, which can cause you to lose track of what you were doing.

Luckily, you can adopt some additional protocols and drop one to help with this problem.

Avoiding OpenIntent

Adopting the OpenIntent protocol provides a quick way to force an intent to open the app in question, but you don’t always need to open the app. So you can remove the OpenIntent protocol and leave the AppIntent protocol.

struct GetSessionDetails: AppIntent {

Updating the Return Type of perform

The perform function of an intent is actually quite flexible. It can return anything from an empty result via result() to a complex result that contains the entity, an intent dialog, and a view. To return a complex SessionEntity result like this, the signature of the perform function would read:

func perform() async throws -> some IntentResult & ReturnsValue<SessionEntity> & ProvidesDialog & ShowsSnippetView {
let snippet = SessionSiriDetailView(session: sessionData)
let dialog = IntentDialog(
      full: """
      The runtime reported for \(sessionToGet.name) is \(sessionToGet.sessionLength ?? "no runtime reported") \
      and has the following description: \(sessionToGet.sessionDescription ?? "no description provided").
      """
      supporting: "Here's the information on the requested session.")

Sharing Data Via Transferable

Core Transferable is a declarative way to describe how your entities can be serialized and deserialized for sharing and data transfer. In iOS 18, you can now make your app entities transferable. This allows you to use Siri and shortcuts to convert entities to other types and send them to other intents on the system. For example, you could convert your entity into a PDF and email it.

import CoreTransferable

extension MyAppEntity: Transferable {
  static var transferRepresentation: some TransferRepresentation {
    DataRepresentation(exportedContentType: .png) { myEntity in 
      //function to return PNG data here
    }
  }
}
struct DisplayToUser: AppIntent {
  @Parameter var item: MyEntity

  @Parameter(title: "Data to display as image", supportedContentTypes: [.jpeg, .png])
  var incomingData: IntentFile

  //......
}

See forum comments
Download course materials from Github
Previous: Introduction Next: Demo