Reputation: 1
I am developing an app with SwiftUI and would like to provide my own Siri shortcuts. I have created my own AppEntities for this. Unfortunately, I can't get the shortcut to work: Whenever I select an entry from the suggestion list that is not the first suggested item, Siri keeps asking for an input.
Has anyone had any experience with this so far or a working code example that I can try out? Thanks already!
I tried to recreate the example from WWDC, but I get the same behaviour. Here is the replica:
struct BookEntity: AppEntity, Identifiable {
var id: UUID
var title : String
var displayRepresentation: DisplayRepresentation {
DisplayRepresentation(stringLiteral: "\(title)")
}
static var typeDisplayRepresentation: TypeDisplayRepresentation = "Book"
static var defaultQuery = BookQuery()
}
struct BookQuery: EntityStringQuery {
func entities(for identifiers: [UUID]) async throws -> [BookEntity] {
let entries = try await suggestedEntities()
return identifiers.compactMap { identifier in
entries.filter{$0.id == identifier}.first
}
}
func suggestedEntities() async throws -> [BookEntity] {
let titles = ["Hallo","Du","Schöne","Welt"]
return titles.map{BookEntity(id: UUID(), title: $0)}
}
func entities(matching string: String) async throws -> [BookEntity] {
return try await suggestedEntities()
}
}
struct OpenBook: AppIntent {
@Parameter(title: "Book")
var book: BookEntity
static var title: LocalizedStringResource = "Open Book"
static var openAppWhenRun = true
@MainActor
func perform() async throws -> some IntentResult {
guard try await $book.requestConfirmation(for: book, dialog: "Are you sure you want to clear read state for \(book)?") else {
return .result()
}
return .result()
}
static var parameterSummary: some ParameterSummary {
Summary("Open \(\.$book)")
}
init() {}
init(book: BookEntity) {
self.book = book
}
}
Upvotes: 0
Views: 353
Reputation: 1034
As a Swift technology expert, here is the English translation of the Chinese response:
When using EntityQuery to retrieve custom AppEntity, please note that suggestedEntities()
and entities()
should use the same set of instance IDs to avoid a scenario where the user selects an element ID from the suggested list, but that ID cannot be found in the entities()
method, forcing the user to re-select.
In your example, the option IDs retrieved from suggestedEntities()
are newly generated UUID()
each time, which is why the entities()
method always returns an empty result. You should ensure the stability of the ID system to avoid this issue.
Upvotes: 0