5

我将经常性事件(EKEvent)写入日历。如何在特定日期获取和修改其中一个经常发生的事件?

这些事件是通过以下代码创建的:

+ (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);
        }
    }];
}

使用-predicateForEventsWithStartDate:endDate:calendars:仅获取日期范围内的事件,而不是特定事件。并且使用事件标识符仅获取一个事件,但没有特定日期。

4

1 回答 1

4

根据文件

重复事件标识符对于所有事件都是相同的。如果您希望区分事件,您可能需要使用开始日期。

获取 a 的特定重现的示例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;
    }
}
于 2014-02-17T14:55:08.900 回答