繁体   English   中英

如何从UILocalNotification对象中获取NEXT开火日期

[英]How to grab the NEXT fire date from a UILocalNotification object

我有一个UILocalNotification对象,我设置了重复间隔日,周和月。 我在访问对象的开火日期时没有任何麻烦:

[cell.detailTextLabel setText:[notification1.fireDate description]];

但是我遇到了下一次火灾日期的麻烦。 如果我将上面的notification1对象打印到控制台,我得到这个:

<UIConcreteLocalNotification: 0x613e060>{fire date = 2010-11-29 03:53:52 GMT, time zone = America/Denver (MST) offset -25200, repeat interval = 16, next fire date = 2010-11-30 03:53:52 GMT}

这个对象包含显示下一个开火日期所需的值或数据...但我找不到它! 有谁知道我可以通过编程方式获得它?

谢谢

要计算重复UILocalNotification下一个触发日期,您必须:

  1. 计算出通知的原始开火日期(即其fireDate属性)和现在之间的repeatInterval数量。
  2. 将它们添加到通知的fireDate

这是一种方法:

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

NSDateComponents *difference = [calendar components:notif.repeatInterval
                                           fromDate:notif.fireDate
                                             toDate:[NSDate date]
                                            options:0];

NSDate *nextFireDate = [calendar dateByAddingComponents:difference
                                                 toDate:notif.fireDate
                                                options:0];

这适用于许多场景,但这是一个不起作用的场景:

假设:

  • 通知的`fireDate是01/01在下午2:00
  • 通知的repeatIntervalNSDayCalendaryUnit (即每天重复)
  • 现在的日期是08/01下午3:00

上面的代码将计算差异为7天(01/01 + 7天= 08/01),将它们添加到fireDate ,从而将nextFireDate设置为08/01在下午2点 但那是在过去,我们希望nextFireDate09/01下午2点

因此,如果使用上面的代码并且您的repeatIntervalNSDayCalendaryUnit ,那么添加以下行:

if ([nextFireDate timeIntervalSinceDate:[NSDate date]] < 0) {
    //next fire date should be tomorrow!
    NSDateComponents *extraDay = [[NSDateComponents alloc] init];
    extraDay.day = 1;
    nextFireDate = [calendar dateByAddingComponents:extraDay toDate:nextFireDate options:0];
}

我将此答案标记为社区维基,如果您找到了更好的计算方法,请随时编辑它!

我不认为下一个开火日期可用作属性,而是从fireDaterepeatInterval计算。 对于不同的时区和其他讨厌的事情,日期计算可能会很棘手。 在您的示例中,您选择了每日重复并计算下一个开火日期,您可以执行以下操作:

NSCalendar *calendar = localNotif.repeatCalendar;
if (!calendar) {
  calendar = [NSCalendar currentCalendar];
}

NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
components.day = 1;
NSDate *nextFireDate = [calendar dateByAddingComponents:components toDate:localnotif.fireDate options:0];

如果您使用其他重复间隔,则必须相应地更改代码。 如果您要使用NSMonthCalendarUnit ,则必须使用components.month = 1

我只想添加repeatInterval,直到日期在未来:

-(NSDate*)nextFireDateForNotification:(UILocalNotification*)notification {
        NSCalendar *calendar = notification.repeatCalendar;
        if (!calendar) {
            calendar = [NSCalendar currentCalendar];
        }

        NSDate* date = [notification.fireDate copy];
        while (date.timeIntervalSinceNow > 0) {
            date = [calendar dateByAddingUnit:notification.repeatInterval value:1 toDate:date options:0];
        }
        return date;
    }

这是在Swift 4中并使用日历的nextDate函数。

extension UILocalNotification {

    var nextFireDate: Date? {
        guard let fireDate = fireDate else { return nil }

        let today = Date()
        let cal = Calendar.current

        if fireDate.compare(today) == .orderedDescending {
            return fireDate
        }

        let s: Set<Calendar.Component>
        switch repeatInterval {
        case .year: s = [.month, .day, .hour, .minute, .second]
        case .month: s = [.day, .hour, .minute, .second]
        case .day: s = [.hour, .minute, .second]
        case .hour: s = [.minute, .second]
        case .minute: s = [.second]
        default: return nil // Not supporting other intervals
        }

        let components = cal.dateComponents(s, from: fireDate)
        return cal.nextDate(after: today, matching: components, matchingPolicy: .nextTimePreservingSmallerComponents)
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM