• <small id='pa0X9'></small><noframes id='pa0X9'>

  • <tfoot id='pa0X9'></tfoot>

          <bdo id='pa0X9'></bdo><ul id='pa0X9'></ul>
        <i id='pa0X9'><tr id='pa0X9'><dt id='pa0X9'><q id='pa0X9'><span id='pa0X9'><b id='pa0X9'><form id='pa0X9'><ins id='pa0X9'></ins><ul id='pa0X9'></ul><sub id='pa0X9'></sub></form><legend id='pa0X9'></legend><bdo id='pa0X9'><pre id='pa0X9'><center id='pa0X9'></center></pre></bdo></b><th id='pa0X9'></th></span></q></dt></tr></i><div id='pa0X9'><tfoot id='pa0X9'></tfoot><dl id='pa0X9'><fieldset id='pa0X9'></fieldset></dl></div>
        <legend id='pa0X9'><style id='pa0X9'><dir id='pa0X9'><q id='pa0X9'></q></dir></style></legend>

        如何在 iOS 14 Home Widget 中显示当前时间(实时)

        时间:2023-09-09
        <tfoot id='LDLoH'></tfoot>
          <tbody id='LDLoH'></tbody>
        <legend id='LDLoH'><style id='LDLoH'><dir id='LDLoH'><q id='LDLoH'></q></dir></style></legend>
          <bdo id='LDLoH'></bdo><ul id='LDLoH'></ul>
          • <i id='LDLoH'><tr id='LDLoH'><dt id='LDLoH'><q id='LDLoH'><span id='LDLoH'><b id='LDLoH'><form id='LDLoH'><ins id='LDLoH'></ins><ul id='LDLoH'></ul><sub id='LDLoH'></sub></form><legend id='LDLoH'></legend><bdo id='LDLoH'><pre id='LDLoH'><center id='LDLoH'></center></pre></bdo></b><th id='LDLoH'></th></span></q></dt></tr></i><div id='LDLoH'><tfoot id='LDLoH'></tfoot><dl id='LDLoH'><fieldset id='LDLoH'></fieldset></dl></div>

            • <small id='LDLoH'></small><noframes id='LDLoH'>

                1. 本文介绍了如何在 iOS 14 Home Widget 中显示当前时间(实时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在为 ios 14 Home Widget 开发应用程序,但在显示当前时间(数字时钟)时遇到问题,该小部件没有每秒更新一次.众所周知,Apple 不允许每秒触发时间轴,有没有其他方法可以通过实时更新显示当前时间?

                  I am developing an application for ios 14 Home Widget and I am facing a problem while showing the current time (digital clock) that the widget not getting updated every sec. As we all know, Apple does not allow to trigger timeline every second is there any other way to display the current time with the realtime updates?

                  我尝试了这些方法,但没有按预期工作

                  I tried these methods but not works as expected

                  class NetworkManager: ObservableObject {
                      @Published var dateIs = Date()
                      
                      init() {
                          startTimer() // fetch data must be called at least once
                      }
                      private  func startTimer() { 
                          let t = RepeatingTimer(timeInterval: 3)
                                  t.eventHandler = {
                                      DispatchQueue.main.async() {
                                          // your UI update code
                                          print("Timer Fired")
                                       self.dateIs = Date()
                                          WidgetCenter.shared.reloadAllTimelines() // reload timelines
                                      }
                                      if(false){   //I know this makes no sense, but it works. Go figure...
                                          t.suspend()
                                      }
                                  }
                                  t.resume()
                  
                      }
                  }
                  
                  
                  struct Provider: TimelineProvider {
                  
                      init() {
                          networkManager = NetworkManager()
                      }
                  
                      
                      @AppStorage("storedData", store: UserDefaults(suiteName: "group.com.name"))
                      var data: Data  = Data()
                      
                      func placeholder(in context: Context) -> WidgetEntry {
                          let entry = WidgetEntry(date: Date(), storedData: storedData[0], dateIs: networkManager.dateIs)
                          return entry
                      }
                      
                      func getSnapshot(in context: Context, completion: @escaping (WidgetEntry) -> Void) {
                          
                          guard let storedData: WidgetTemplate = try? JSONDecoder().decode(WidgetTemplate.self, from: data) else { return }
                          let entry = WidgetEntry(date: Date(), storedData: storedData, dateIs: networkManager.dateIs)
                            
                          completion(entry)
                      }
                      
                      func getTimeline(in context: Context, completion: @escaping (Timeline<WidgetEntry>) -> Void) {
                      
                          guard let storedData: WidgetTemplate = try? JSONDecoder().decode(WidgetTemplate.self, from: data) else { return }
                          let entry = WidgetEntry(date: Date(), storedData: storedData, dateIs: networkManager.dateIs)
                          
                          let currentDate = Date()
                          let refreshDate = Calendar.current.date(byAdding: .minute, value: 5, to: currentDate)!
                          
                          let timeline = Timeline(entries: [entry], policy:  .after(refreshDate))
                          completion(timeline)
                      }
                      
                  }
                  

                  推荐答案

                  您不能每秒刷新 Widget.这就是 Apple 的限制.

                  You’re not allowed to refresh the Widget every second. That’s the limitation from Apple.

                  你能做的最好的就是用显示一个日期使用 timer 日期样式:

                  Best you can do is display a date with seconds using the timer date style:

                  /// A style displaying a date as timer counting from now.
                  ///
                  ///     Text(event.startDate, style: .timer)
                  ///
                  /// Example output:
                  ///    2:32
                  ///    36:59:01
                  public static let timer: Text.DateStyle
                  


                  1. 您需要一个带有 Date 属性的简单 Entry:
                  1. You need a simple Entry with a Date property:

                  struct SimpleEntry: TimelineEntry {
                      let date: Date
                  }
                  

                  1. Entry中传递给定日期的午夜日期,并在下一个午夜之后刷新时间线:
                  1. Pass the midnight date for a given day in the Entry and refresh the timeline after the next midnight:

                  struct SimpleProvider: TimelineProvider {
                      ...
                  
                      func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> Void) {
                          let midnight = Calendar.current.startOfDay(for: Date())
                          let nextMidnight = Calendar.current.date(byAdding: .day, value: 1, to: midnight)!
                          let entries = [SimpleEntry(date: midnight)]
                          let timeline = Timeline(entries: entries, policy: .after(nextMidnight))
                          completion(timeline)
                      }
                  }
                  

                  1. 使用 timer 样式显示日期:
                  1. Display the date using the timer style:

                  struct SimpleWidgetEntryView: View {
                      var entry: SimpleProvider.Entry
                  
                      var body: some View {
                          Text(entry.date, style: .timer)
                      }
                  }
                  


                  请注意,当您刷新时间线时(通过指定 TimelineReloadPolicy 或通过调用 WidgetCenter),您将设置 earliest Widget 刷新的时间.


                  Note that when you refresh the timeline (either by specifying the TimelineReloadPolicy or by calling the WidgetCenter) you set the earliest time when the Widget will be refreshed.

                  不保证在特定时间刷新.

                  It is not guaranteed to be refreshed at that specific time.

                  这篇关于如何在 iOS 14 Home Widget 中显示当前时间(实时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Android 小部件未更新 - 小部件 ID 不正确 下一篇:Android:如何在创建时更新小部件文本

                  相关文章

                    <small id='N1HdY'></small><noframes id='N1HdY'>

                    <i id='N1HdY'><tr id='N1HdY'><dt id='N1HdY'><q id='N1HdY'><span id='N1HdY'><b id='N1HdY'><form id='N1HdY'><ins id='N1HdY'></ins><ul id='N1HdY'></ul><sub id='N1HdY'></sub></form><legend id='N1HdY'></legend><bdo id='N1HdY'><pre id='N1HdY'><center id='N1HdY'></center></pre></bdo></b><th id='N1HdY'></th></span></q></dt></tr></i><div id='N1HdY'><tfoot id='N1HdY'></tfoot><dl id='N1HdY'><fieldset id='N1HdY'></fieldset></dl></div>

                    <tfoot id='N1HdY'></tfoot>
                    • <bdo id='N1HdY'></bdo><ul id='N1HdY'></ul>

                    <legend id='N1HdY'><style id='N1HdY'><dir id='N1HdY'><q id='N1HdY'></q></dir></style></legend>