Xaree Lee
Xaree Lee

Reputation: 3397

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.

Upvotes: 5

Views: 2409

Answers (1)

Laszlo
Laszlo

Reputation: 2778

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;
    }
}

Upvotes: 4

Related Questions