Is this not possible? I have a simple text view that shows the time, but it doesn't stay in sync with the time. I've tried to use a timer like I do inside the app, but that didn't work. I tried TimelineView with it set to every minute, and that didn't work. I tried to use the dynamic date using Text(date, style: )
and that didn't work. I looked it up and apparently it's a limitation to WidgetKit, however, I don't understand how the Apple Clock widget can have a moving second hand that updates properly.
Do I just need to add a bunch of entries in the TimelineProvider? Right now, it's just the default that gets loaded when you create a Widget extension.
func timeline(for configuration: SingleClockIntent, in context: Context) async -> Timeline<SingleClockEntry> { var entries: [SingleClockEntry] = [] // Generate a timeline consisting of five entries an hour apart, starting from the current date. let currentDate = Date() for hourOffset in 0 ..< 5 { let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)! let entry = SingleClockEntry(date: entryDate, configuration: configuration) entries.append(entry) } return Timeline(entries: entries, policy: .atEnd) }
EDIT: This code below seems to be working. Can someone confirm if it will continue to work, or will it eventually stop updating?
func timeline(for configuration: SingleClockIntent, in context: Context) async -> Timeline<SingleClockEntry> { var entries: [SingleClockEntry] = [] let calendar = Calendar.current let current = Date() // Get the next minute let secondEntryDate = calendar.nextDate(after: current, matching: DateComponents(second: 0), matchingPolicy: .strict, direction: .forward)! entries.append(SingleClockEntry(date: current, configuration: configuration)) // Adds current time entries.append(SingleClockEntry(date: secondEntryDate, configuration: configuration)) // Adds next minute after current time // Add an entry every min for the next 30 minutes for offset in 0..<30 { let entryDate = Calendar.current.date(byAdding: .minute, value: offset, to: secondEntryDate)! let entry = SingleClockEntry(date: entryDate, configuration: configuration) entries.append(entry) } return Timeline(entries: entries, policy: .atEnd) }