Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
685 views
in Technique[技术] by (71.8m points)

swift - 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 {
  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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...