Swift3 如果fireDate日>;如何触发UILocalNotification;月的最后一天?
专家们,任何帮助都将不胜感激 我将UILocalNotification设置为1月30日下午2:00,它每月重复一次(Swift3 如果fireDate日>;如何触发UILocalNotification;月的最后一天?,swift3,uilocalnotification,repeat,Swift3,Uilocalnotification,Repeat,专家们,任何帮助都将不胜感激 我将UILocalNotification设置为1月30日下午2:00,它每月重复一次(localNotification.repeatInterval=NSCalendar.Unit.month)。它将在3月30日下午2:00触发(所有这些都非常有效) 但是在二月份,由于没有每月的第30天,本地通知将不会触发 如果fireDate大于当月最后一天,我是否可以在当月最后一天触发通知 编辑 以下是基于@MirekE建议的部分解决方案: func application
localNotification.repeatInterval=NSCalendar.Unit.month
)。它将在3月30日下午2:00触发(所有这些都非常有效)
但是在二月份,由于没有每月的第30天,本地通知将不会触发
如果fireDate大于当月最后一天,我是否可以在当月最后一天触发通知
编辑
以下是基于@MirekE建议的部分解决方案:
func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
print("comparing fireDate to nextMonthLastDayDate..")
//get the fire date of the notification received
let notificationFireDate = notification.fireDate
//get the last day of the next month
let now = Date()
let calendar = Calendar.current
guard let days = calendar.range(of: .day, in: .month, for: now) else { preconditionFailure("Range of days can't be calculated") }
let lastDay = days.upperBound - 1
var lastDayComponents = calendar.dateComponents([.day, .month, .year], from: now)
lastDayComponents.day = lastDay
guard let lastDayDate = calendar.date(from: lastDayComponents) else { preconditionFailure("Date can't be calculated from components") }
let nextMonthLastDayDate = calendar.date(byAdding: .month, value: 1, to: lastDayDate)
//if the firedate is greater than the last day fo the next month...
if notificationFireDate! > nextMonthLastDayDate! {
//then set the firedate to the last day of the next month
notification.fireDate = nextMonthLastDayDate
print("did set fireDate = nextMonthLastDayDate : \(notification.fireDate)")
} else {
print("fireDate < nextMonthLastDayDate. fireDate not changed : \(notification.fireDate)")
}
}
func应用程序(application:UIApplication,didReceive通知:UILocalNotification){
打印(“将fireDate与nextMonthLastDayDate…进行比较”)
//获取收到通知的火灾日期
让notificationFireDate=notification.fireDate
//得到下个月的最后一天
让我们现在=日期()
让calendar=calendar.current
guard let days=calendar.range(of:.day,in:.month,for:now)else{PremissionFailure(“无法计算天数范围”)}
设lastDay=days.upperBound-1
var lastDayComponents=calendar.dateComponents([.day、.month、.year],from:now)
lastDayComponents.day=最后一天
guard let lastDayDate=calendar.date(from:lastDayComponents)else{PremissionFailure(“日期不能从组件计算”)}
让nextMonthLastDayDate=calendar.date(通过添加:.month,值:1,到:lastDayDate)
//如果firedate大于下个月的最后一天。。。
如果通知FireDate!>nextMonthLastDayDate{
//然后将点火日期设置为下个月的最后一天
notification.fireDate=nextMonthLastDayDate
打印(“did set fireDate=nextMonthLastDayDate:\(notification.fireDate)”)
}否则{
打印(“fireDate
现在,当收到原定于1月30日(每月重复)发出的通知时,新的点火日期将更改为2月28日
但是,它将在3月28日(而不是3月30日)开火。关于如何确保这一点,您有什么想法吗?根据您想做什么,您可以考虑: 将当前日期表示为组件,然后将
.day
替换为最后一天的值,并将其转换回日期
和/或使用日历.日期(起始日期:DateComponents…
谢谢你的建议。我在上面做了一些编辑。现在,当收到原定于1月30日(每月重复)发出的通知时,新的点火日期将改为2月28日。但是,它将在3月28日(而不是3月30日)开火。在2月份设置为28之后,有没有办法使3月份的fireDate=30?
let now = Date()
let calendar = Calendar.current
guard let days = calendar.range(of: .day, in: .month, for: now) else { preconditionFailure("Range of days can't be calculated") }
let lastDay = days.upperBound - 1
var lastDayComponents = calendar.dateComponents([.day, .month, .year], from: now)
lastDayComponents.day = lastDay
guard let lastDayDate = calendar.date(from: lastDayComponents) else { preconditionFailure("Date can't be calculated from components") }
let nextMonthLastDayDate = calendar.date(byAdding: .month, value: 1, to: lastDayDate)