从EkCalendar iOS获取过去的活动

时间:2015-03-23 15:25:12

标签: ios nsdate

我正试图从特定的日历(EkCalendar)中获取过去的事件,但我能获得的最古老的事件是从1年前开始的。有没有办法让事件超过一年?

注意 有关该主题的一些问题,但答案不是很清楚或似乎不起作用。 例如:Past events in EKCalendar

谢谢

1 个答案:

答案 0 :(得分:0)

我终于找到了解决方案以及为什么我无法通过事件的原因。显然,iOS不允许在超过4年的谓词上获取事件。

答案如下: Fetch all events from EventStore EventKit iOS

NSDate *start = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:currentDate options:0];
NSDate *finish = [NSDate new];
NSArray *calendarArray = [NSArray arrayWithObject:calendar];


NSMutableDictionary *dicEvents = [NSMutableDictionary dictionaryWithCapacity:1024];

NSDate *currentStart = [NSDate dateWithTimeInterval:0 sinceDate:start];

int seconds_in_year = 60 * 60 * 24 * 365;

while ([currentStart compare:finish] == NSOrderedAscending) {
    NSDate *currentFinish = [NSDate dateWithTimeInterval:seconds_in_year sinceDate:currentStart];

    if ([currentFinish compare:finish] == NSOrderedDescending) {
        currentFinish = [NSDate dateWithTimeInterval:0 sinceDate:finish];
    }
    NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:currentStart endDate:currentFinish calendars:calendarArray];
    [self.eventStore enumerateEventsMatchingPredicate:predicate
                                           usingBlock: ^(EKEvent *event, BOOL *stop) {
                                               if (event) {
                                                   [dicEvents setObject:event forKey:event.eventIdentifier];
                                               }
                                           }];
    currentStart = [NSDate dateWithTimeInterval:(seconds_in_year + 1) sinceDate:currentStart];
}
return [dicEvents allValues];