gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:32:55 25 4
gpt4 key购买 nike

我正在为 ios 14 Home Widget 开发一个应用程序,我在显示当前时间(数字时钟)时遇到了一个问题,即该 WidgetKit 没有每秒钟更新一次。众所周知,Apple不允许每秒触发时间线,还有其他方法可以实时更新显示当前时间吗?
我尝试了这些方法,但没有按预期工作

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。这是苹果的限制。
你能做的最好的事情是显示一个带秒的日期
使用 timer日期样式:

/// 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
  • 您需要一个简单的 EntryDate属性(property):
  • struct SimpleEntry: TimelineEntry {
    let date: Date
    }
  • Entry 中传递给定日期的午夜日期并在下一个午夜之后刷新时间线:
  • 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)
    }
    }
  • 使用 timer 显示日期款式:
  • struct SimpleWidgetEntryView: View {
    var entry: SimpleProvider.Entry

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

    请注意,当您刷新时间线时(通过指定 TimelineReloadPolicy 或通过调用 WidgetCenter )您设置了 最早 Widget 刷新的时间。
    不保证刷新 在那个特定的时间 .

    关于swift - 如何在 iOS 14 Home Widget 中显示当前时间(实时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64053270/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com