0

I'm trying to get past events from a specific Calendar (EkCalendar), but the oldest events I can get is from 1 year ago. Is there a way to get events older than just a year?

NOTE Theres some questions about the subject but the answers are not very clear or doesn't seems to work. ex: Past events in EKCalendar

Thank you

Community
  • 1
  • 1
Hugo
  • 295
  • 2
  • 15

1 Answers1

0

I finally found the solution and the reason of why I was unable to get past events. Apparently, iOS doesn't allow to get events on a predicate longer than 4 years.

Here the answer : 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];
Community
  • 1
  • 1
Hugo
  • 295
  • 2
  • 15