Widgets & Live Activities

RSS for tag

Discuss how to manage and implement Widgets & Live Activities.

WidgetKit Documentation

Posts under Widgets & Live Activities subtopic

Post

Replies

Boosts

Views

Activity

App Intent Parameter (AppEntity) not registering
I'm currently testing this on a physical device (12 Pro Max, iOS 26). Through shortcuts, I know for a fact that I am able to successfully trigger the perform code to do what's needed. In addition, if I just tell siri the phrase without my unit parameter, and it asks me which unit, I am able to, once again, successfully call my perform. The problem is any of my phrases that I include my unit, it either just opens my application, or says "I can't understand" Here is my sample code: My Entity: import Foundation import AppIntents struct Unit: Codable, Identifiable { let nickname: String let ipAddress: String let id: String } struct UnitEntity: AppEntity { static var typeDisplayRepresentation: TypeDisplayRepresentation { TypeDisplayRepresentation( name: LocalizedStringResource("Unit", table: "AppIntents") ) } static let defaultQuery = UnitEntityQuery() // Unique Identifer var id: Unit.ID // @Property allows this data to be available to Shortcuts, Siri, Etc. @Property var name: String // By not including @Property, this data is NOT used for queries. var ipAddress: String var displayRepresentation: DisplayRepresentation { DisplayRepresentation( title: "\(name)" ) } init(Unit: Unit) { self.id = Unit.id self.ipAddress = Unit.ipAddress self.name = Unit.nickname } } My Query: struct UnitEntityQuery: EntityQuery { func entities(for identifiers: [UnitEntity.ID]) async throws -> [UnitEntity] { print("[UnitEntityQuery] Query for IDs \(identifiers)") return UnitManager.shared.getUnitUnits() .map { UnitEntity(Unit: $0) } } func suggestedEntities() async throws -> [UnitEntity] { print("[UnitEntityQuery] Request for suggested entities.") return UnitManager.shared.getUnitUnits() .map { UnitEntity(Unit: $0) } } } UnitsManager: class UnitManager { static let shared = UnitManager() private init() {} var allUnits: [UnitEntity] { getUnitUnits().map { UnitEntity(Unit: $0) } } func getUnitUnits() -> [Unit] { guard let jsonString = UserDefaults.standard.string(forKey: "UnitUnits"), let data = jsonString.data(using: .utf8) else { return [] } do { return try JSONDecoder().decode([Unit].self, from: data) } catch { print("Error decoding units: \(error)") return [] } } func contactUnit(unit: UnitEntity) async -> Bool { // Do things here... } } My AppIntent: import AppIntents struct TurnOnUnit: AppIntent { static let title: LocalizedStringResource = "Turn on Unit" static let description = IntentDescription("Turn on an Unit") static var parameterSummary: some ParameterSummary { Summary("Turn on \(\.$UnitUnit)") } @Parameter(title: "Unit Unit", description: "The Unit Unit to turn on") var UnitUnit: UnitEntity func perform() async throws -> some IntentResult & ProvidesDialog { //... My code here } } And my ShortcutProvider: import Foundation import AppIntents struct UnitShortcuts: AppShortcutsProvider { static var appShortcuts: [AppShortcut] { AppShortcut( intent: TurnOnUnit(), phrases: [ "Start an Unit with \(.applicationName)", "Using \(.applicationName), turn on my \(\.$UnitUnit)", "Turn on my \(\.$UnitUnit) with \(.applicationName)", "Start my \(\.$UnitUnit) with \(.applicationName)", "Start \(\.$UnitUnit) with \(.applicationName)", "Start \(\.$UnitUnit) in \(.applicationName)", "Start \(\.$UnitUnit) using \(.applicationName)", "Trigger \(\.$UnitUnit) using \(.applicationName)", "Activate \(\.$UnitUnit) using \(.applicationName)", "Volcano \(\.$UnitUnit) using \(.applicationName)", ], shortTitle: "Turn on unit", systemImageName: "bolt.fill" ) } }
1
0
27
1d
Widget with Core Data guidance
I have an app that has a Core Data store for dates with descriptions that I'd like to present in a widget with countdown calculations. In the app I have a button that just equates an active calculation to the currently selected item in the database (using an EnvironmentObject). I gather I can't use this mechanism inside a widget, right? The user could put tons of items into the database - so I'm sure I don't want to have an editable widget allowing them to pick. I suppose I could create an intent and allow an independent entering from the app - but that seems rather user hostile since they've already entered it for the app - and I'm still trying to support iOS15 which doesn't support that. I did create an App Group and have the Core Data store available from within the widget, but I don't see how to allow the user to choose which date is active. I also want multiple widgets to be able to point to different dates. Any help would be appreciated. Thanks!
2
0
216
3h