检查今天是否是新的一天 SwiftUI



如何检查今天是否是新的一天? 我怎么能说应用程序何时午餐或在后台,如果是新一天的早上 8 点,然后做一些动作......

Date()只会给我当前日期。

我需要它进行一些重置,并可能在特定时间发送通知

你可以尝试这样的事情:

import SwiftUI
struct ContentView: View {
@State var lastDay: Date = Date()
@State var isToday = false
@State var selectedTime = 8  // 24 hour clock
var body: some View {
Text("your main view")
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willResignActiveNotification)) { _ in
// store the date whenever you go into background
print("---> storing: (Date())")
UserDefaults.standard.set(Date(), forKey: "lastDay")
}
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
// try to retrieve the date when you come back from background
print("n----> try retrieve lastDay")
if let temDate = UserDefaults.standard.object(forKey: "lastDay") {
self.lastDay = temDate as! Date
print("----> retrieved lastDay: (self.lastDay)")
if Calendar.current.isDate(Date(), inSameDayAs: self.lastDay) {
self.isToday = true
print("----> isToday: (self.isToday)n")
// if it is 8 am or later do something
if let thisHour = Calendar.current.dateComponents([.hour], from: Date()).hour {
if (thisHour >= self.selectedTime) {
print("----> it is 8am or later --> do something")
// self.doSomething()
}
}
}
}
}
}
}

通知中心是苹果内部消息系统。SwiftUI 可以侦听特定事件,例如当应用程序进入后台时。这是此行的作用:

.onReceive(NotificationCenter.default.publisher(for: UIApplication.willResignActiveNotification)) 

现在,这一行:

.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) 

如评论中所述,当您从后台回来时会倾听。

由于这些onReceives,没有减速。

我展示的是一种方法,您可以添加其他 .onReceive,例如:

.onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willTerminateNotification)) { _ in

这些可用于启动应用程序时和退出应用程序时。

所以这一切都应该能够判断日期是否已更改。

struct ContentView: View {
@State private var lastDateString = UserDefaults.standard.string(forKey: "lastDateString") ?? String()
//stores lastDateString in memory for if the app closes
@State var lastDate = Date()
@State var currentDate = Date()
@State var currentDateString = String()
@State var differentDate = false;
let timer = Timer.publish(every: 1.0, on: .main, in: .common).autoconnect()
//Creates a timer that goes every 1 second 
func newDay() {
let formatter = DateFormatter()
formatter.dateFormat = "d MM y"
//sets the format so it will be day month year
if lastDateString == String() {
lastDateString = formatter.string(from: lastDate)
UserDefaults.standard.set(self.lastDateString, forKey: "lastDateString")
} //sets initial value for lastDateString for first time app ever launches
self.currentDate = Date()
currentDateString = formatter.string(from: currentDate)
//sets currentDateString for every time app launches
if lastDateString != currentDateString {
self.differentDate = true
self.lastDate = Date()
self.currentDate = Date()
lastDateString = formatter.string(from: lastDate)
UserDefaults.standard.set(self.lastDateString, forKey: "lastDateString")
currentDateString = formatter.string(from: currentDate)
} 
//checks if the date has changed and sets differentDate to true if it has
}
var body: some View {
VStack{
Text("Hello, World!")
.onAppear(){ //used to run function when app launches
self.newDay()
}
}
.onReceive(self.timer) { _ in
self.newDay()
} //calls function newDay() every second from timer

}
}

希望这有帮助,很抱歉这可能不是最简洁的,但希望它有所帮助。

if Calendar.current.isDate(lastVisit as! Date, inSameDayAs: Date()) {
print("Same day")
} else {
print("New day")
}

最新更新