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.
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With