• <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 中显示当前时间(实时)

        How to display Current Time (Realtime) in iOS 14 Home Widget(如何在 iOS 14 Home Widget 中显示当前时间(实时))
        <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 中显示当前时间(实时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  reloadData() of UITableView with Dynamic cell heights causes jumpy scrolling(具有动态单元格高度的 UITableView 的 reloadData() 导致跳跃滚动)
                  How to format localised strings in Swift?(如何在 Swift 中格式化本地化字符串?)
                  Why do we need to set delegate to self? Why isn#39;t it defaulted by the compiler?(为什么我们需要将委托设置为自我?为什么编译器不默认它?)
                  set delegate and datasource of uitableview programmatically in Swift(在 Swift 中以编程方式设置 uitableview 的委托和数据源)
                  Swift delegate beetween two VC without segue(两个没有 segue 的 VC 之间的 Swift 委托)
                  Swift can#39;t call protocol method via delegate(Swift 不能通过委托调用协议方法)

                    <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>
                      <tbody id='N1HdY'></tbody>
                        <bdo id='N1HdY'></bdo><ul id='N1HdY'></ul>

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