在 SWIFT 中获取"change of day"事件的有效方式是什么



所以我有一个swift ui应用程序,我在其中显示当天的信息-所以在午夜我希望它切换到新的一天-问题是我希望它像";"轻";尽可能-例如,我不希望任何东西消耗任何资源,因为我只想刷新视图(如果显示的话(。

当视图未显示时,如何在不消耗后台资源的情况下做到这一点?

您可以尝试以下操作:(注意,我没有等着看这是否有效(

import SwiftUI
import Combine
#if os(iOS)
import UIKit
#endif

@main
struct TestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
var body: some View {
Text("day change")
.onReceive(NotificationCenter.default.publisher(
for: UIApplication.significantTimeChangeNotification)) { _ in

print("----> day time has changed <----n")

print("A notification that posts when there is a significant change in time, n for example, change to a new day (midnight), n carrier time update, and change to or from daylight savings time.")
}
}
}

相关内容

最新更新