I am trying to include AppIntents
into my app. To achieve this, I am following Apple‘s WWDC Session. Everything works great until I try to add a parameter to my intent.
My Entity
and Query
look like this:
struct ActivityEntity: AppEntity {
var id: String
var name: String
static var typeDisplayRepresentation: TypeDisplayRepresentation = .init(name: "Activity")
var displayRepresentation: DisplayRepresentation {
DisplayRepresentation(title: LocalizedStringResource(stringLiteral: self.name))
}
static var defaultQuery: MobilityActivityQuery = MobilityActivityQuery()
}
struct MobilityActivityQuery: EntityQuery {
func entities(for identifiers: [String]) async throws -> [ActivityEntity] {
Activity.all()?.compactMap({ activity in
identifiers.contains(where: { $0 == activity.name }) ? ActivityEntity(id: activity.name, name: activity.name) : nil
}) ?? []
}
}
My Intent
looks like this:
struct StartSessionIntent: AppIntent {
static var title: LocalizedStringResource = "Start Recording"
@Parameter(title: "Activity")
var activity: ActivityEntity?
func perform() async throws -> some IntentResult & ProvidesDialog {
var activityToSelect: ActivityEntity? = self.activity
if activityToSelect == nil {
activityToSelect = try await self.$activity.requestDisambiguation(among: Activity.all()?.map { ActivityEntity(id: $0.name, name: $0.name) } ?? [])
}
guard let selectedActivity = Activity[activityToSelect?.name ?? ""] else {
return .result(dialog: "Failed to get activity")
}
Session.shared.setActivity(activity: selectedActivity)
Session.shared.startSession()
return .result(dialog: "Okay, starting to record this mobility")
}
}
As soon as the perform
method accesses activity
, the execution fails and I get the notification from Shortcuts saying: The action contains invalid metadata
.
In the console the only message I get is this: [Execution] Unknown NSError Communication with the Helping-App is not possible.
(this might be a bad translation from me)
I am completely clueless on how I can debug this problem and my internet search didn‘t help either. Any help is much appreciated. Thanks in advance
I finally found a solution by exploring this example app: https://iosexample.com/a-demo-app-exploring-the-new-app-intents-framework-in-ios16/.
Here is my new (working) implementation of ActivityEntity
:
struct ActivityEntity: AppEntity {
static var typeDisplayRepresentation = TypeDisplayRepresentation(name: "Activity")
typealias DefaultQuery = MobilityActivityQuery
static var defaultQuery: MobilityActivityQuery = MobilityActivityQuery()
var id: String
var name: String
var displayRepresentation: DisplayRepresentation {
DisplayRepresentation(title: "\(self.name)")
}
}