I'm making a widget app that shows today's date and calendar.
I want the widget refresh every midnight, so I tried several methods.
But eventually I failed to figure out how to do it.
- First try : give entries that update every midnight. But it doesn't update the widget every midnight exactly.
struct Provider: TimelineProvider {
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> Void) {
var entries: [SomeEntry] = []
let currentDate = Date()
let currentEntry = SomeEntry(date: currentDate), content: content)
entries.append(currentEntry)
for offset in 1 ..< 5 {
let day = Calendar.autoupdatingCurrent.date(byAdding: .day, value: offset, to: currentDate)!
let midnight = Calendar.autoupdatingCurrent.startOfDay(for: day)
let entry = SomeEntry(date: midnight, content: content)
entries.append(entry)
}
completion(entries, .atEnd)
}
}
- Second try : Dynamic Dates
I tried to use dynamic dates which was described here (https://developer.apple.com/documentation/widgetkit/displaying-dynamic-dates)
But it's not customizable, so I think I can't use it when making calendar widget.
- Third try : Local notification
I tried to use local notification to reload widget every midnight.
But I found in iOS, I can't use silent local notification.
- Fourth try : Background Tasks
I tried background tasks, but it won't refresh widget if the app is terminated.
I know that other popular widget app's widget updates exact every midnight. Even if I manually change device time, they work.
I think there is a way that can alert widget extension when date changes.
Any idea how to do it??
question from:
https://stackoverflow.com/questions/65896655/ios14-widget-update-every-midnight 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…