如何在午夜不触发applicationSignificantTimeChange,但在所有其他情况下使用它?
这可以按预期工作,但是如何在午夜或23:30至00:30之间将其停止?
func applicationSignificantTimeChange(_ application: UIApplication) {
NSLog("Application Significant Time Change")
Manager.setupTime()
}
答案 0 :(得分:1)
您无法阻止applicationSignificantTimeChange
在午夜触发。
时间发生重大变化的示例包括午夜的到来,承运人对时间的更新以及夏令时的更改。
解决方法:
您可以通过下面的条件来检查当前时间是否在23:30-00:30之间,并忽略它。
func applicationSignificantTimeChange(_ application: UIApplication) {
let currentTime = Date()
let midnight = Calendar.current.startOfDay(for: currentTime)
let thirtyMinsBeforeMidnight = midnight.addingTimeInterval(-30*60)
let thirtyMinutesAfterMidnight = midnight.addingTimeInterval(30*60)
if currentTime.timeIntervalSince(thirtyMinsBeforeMidnight) >= 0 && currentTime.timeIntervalSince(thirtyMinutesAfterMidnight) <= 0 {
// time between 23:30 - 00:30
}
else {
// time is not between 23:30 - 00:30
// do your stuff
NSLog("Application Significant Time Change")
Manager.setupTime()
}
}