A newly created Widget extension, with the default code created by Xcode will continuously call the timeline() function while the widget is being debugged.
Code provided below:
import WidgetKit
import SwiftUI
struct Provider: TimelineProvider {
		public typealias Entry = SimpleEntry
		public func snapshot(with context: Context, completion: @escaping (SimpleEntry) -> ()) {
				NSLog("getting snapshot")
				let entry = SimpleEntry(date: Date())
				completion(entry)
		}
		public func timeline(with context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
				NSLog("getting timeline")
				var entries: [SimpleEntry] = []
				let currentDate = Date()
				for hourOffset in 0 ..< 5 {
						let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)!
						let entry = SimpleEntry(date: entryDate)
						entries.append(entry)
				}
				NSLog("\(entries)")
				let timeline = Timeline(entries: entries, policy: .atEnd)
				completion(timeline)
		}
}
struct SimpleEntry: TimelineEntry {
		public let date: Date
}
struct PlaceholderView : View {
		var body: some View {
				Text("Placeholder View")
		}
}
struct SampleWidgetEntryView : View {
		var entry: Provider.Entry
		var body: some View {
				VStack {
						Text(entry.date, style: .time)
				}
		}
}
@main
struct SampleWidget: Widget {
		private let kind: String = "SampleWidget"
		public var body: some WidgetConfiguration {
				StaticConfiguration(kind: kind, provider: Provider(), placeholder: PlaceholderView()) { entry in
						SampleWidgetEntryView(entry: entry)
				}
				.configurationDisplayName("My Widget")
				.description("This is an example widget.")
		}
}
Post
Replies
Boosts
Views
Activity
I'm trying to display a countdown in my widget, similar to the one shown in the WWDC video on WidgetKit. The problem is that as the time changes the font size is not readjusting and the text is getting cut off. Is there a workaround for this behavior?
Here is my code:
VStack(alignment: .leading) {
		Text("\(entry.name) is in")
				.font(.subheadline)
Text(entry.next.time, style: .relative)
.font(.title2)
	 .lineLimit(1)
	 .frame(maxWidth: .infinity, alignment: .leading)
}
.minimumScaleFactor(0.5)
.allowsTightening(true)
I have a widget that uses a "countdown" or "timer" by using a Text view of a date with a style of relative. The first time this widget is viewed on the Today screen in a locked state it gets the correct data, however all subsequent viewing of the widget from a locked device does not change the "countdown" at all.
Even locking, viewing the now correct today screen and then locking again, will go back to the data it seems to have cached on the first viewing of that widget from the locked state.
Is there anyway for this relative date to update while the device is locked?