Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a specific recurrent EKEvent in the future date?

I wrote recurrent events (EKEvent) into Calendar. How can I get and modify one of those recurrent events in a specific date?

The events were created by following code:

+ (void) writeTestEvent{

    EKEventStore *eventStore = [[EKEventStore alloc] init];
    EKEvent *event = [EKEvent eventWithEventStore:eventStore];

    event.calendar = [eventStore defaultCalendarForNewEvents];
    event.title = @"Hello World!";
    event.startDate = [NSDate date];
    event.endDate = [NSDate date];

    EKRecurrenceRule *recurrenceRule = [[EKRecurrenceRule alloc] initRecurrenceWithFrequency:EKRecurrenceFrequencyDaily interval:1 end:nil];
    [event addRecurrenceRule:recurrenceRule];

    [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        if (granted) {
            BOOL isSaved = [eventStore saveEvent:event span:EKSpanFutureEvents commit:YES error:&error];
            NSLog(@"isSaved: (%d) with error: %@", isSaved, error);
        } else {
            NSLog(@"not granted with error: %@", error);
        }
    }];
}

Using -predicateForEventsWithStartDate:endDate:calendars: only gets events falling within a date range, not a specific event. And using event identifier gets only one event, but not with specific date.

like image 820
Xaree Lee Avatar asked Oct 02 '22 18:10

Xaree Lee


1 Answers

According to documentation:

Recurring event identifiers are the same for all occurrences. If you wish to differentiate between occurrences, you may want to use the start date.

Sample to get the specific recurrence of an EKEvent:

EKEventStore *eventStore = [[EKEventStore alloc] init];

NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:calendars];

NSArray *results =  [eventStore eventsMatchingPredicate:predicate];
for (int i = 0; i < results.count; i++) {
     EKEvent *event = [results objectAtIndex:i]
     if ([event. eventIdentifier isEqualToString: eventIdentifier]) {
        // Match!!
    break;
    }
}
like image 181
Laszlo Avatar answered Oct 07 '22 19:10

Laszlo