Post

Replies

Boosts

Views

Activity

iOS14 Widget update every midnight
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 { &#9;func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> Void) { &#9;&#9;var entries: [SomeEntry] = [] &#9;&#9;let currentDate = Date() &#9;&#9;let currentEntry = SomeEntry(date: currentDate), content: content) &#9;&#9;entries.append(currentEntry) &#9;&#9;for offset in 1 ..< 5 { &#9;&#9;&#9;let day = Calendar.autoupdatingCurrent.date(byAdding: .day, value: offset, to: currentDate)! &#9;&#9;&#9;let midnight = Calendar.autoupdatingCurrent.startOfDay(for: day) &#9;&#9;&#9;let entry = SomeEntry(date: midnight, content: content) &#9;&#9;&#9;entries.append(entry) &#9;&#9;} &#9;&#9;completion(entries, .atEnd) &#9;} } 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??
0
0
842
Jan ’21