如何在未来的日期获得特定的经常性EKEvent?

时间:2013-10-04 08:31:42

标签: ekevent ekeventkit ekeventstore

我将重复事件(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:只会使事件落在日期范围内,而不是特定事件。使用事件标识符只能获得一个事件,但不能获取特定日期。

1 个答案:

答案 0 :(得分:3)

根据documentation

  

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

获取EKEvent特定重现的示例:

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