gpt4 book ai didi

ios - 如何在午夜不触发 applicationSignificantTimeChange

转载 作者:行者123 更新时间:2023-11-28 13:47:12 24 4
gpt4 key购买 nike

如何在午夜不触发 applicationSignificantTimeChange,但在所有其他情况下使用它?

这按预期工作,但如何在午夜或之间(23:30 至 00:30)停止它?

func applicationSignificantTimeChange(_ application: UIApplication) {
NSLog("Application Significant Time Change")
Manager.setupTime()
}

最佳答案

您无法阻止 applicationSignificantTimeChange 在午夜触发。

From documentation:

Examples of significant time changes include the arrival of midnight, an update of the time by a carrier, and the change to daylight savings time.

解决方法:

您可以像下面这样输入 if 条件来检查当前时间是否在 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()
}
}

关于ios - 如何在午夜不触发 applicationSignificantTimeChange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55268897/

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