6

如果我想EKEventEKEventStorewitheventWithIdentifier方法中检索以前保存的事件,但我总是得到空值。

这是添加事件的代码:

EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *newEvent = [EKEvent eventWithEventStore:eventStore];
newEvent.title = @"Test";
newEvent.availability = EKEventAvailabilityFree;
newEvent.startDate = startDate;
newEvent.endDate = endDate;
[newEvent addAlarm:[EKAlarm alarmWithRelativeOffset:-15*60]];

newEvent.calendar = [eventStore defaultCalendarForNewEvents];

NSError *err;
BOOL success = [eventStore saveEvent:newEvent span:EKSpanThisEvent commit:YES error:&err];

if (success) {
    if ([newEvent respondsToSelector:@selector(calendarItemIdentifier)]) {
        [[NSUserDefaults standardUserDefaults] setObject:newEvent.calendarItemIdentifier forKey:self.showId];
        NSLog(@"Event ID: %@",newEvent.calendarItemIdentifier);
    }
    else {
        [[NSUserDefaults standardUserDefaults] setObject:newEvent.UUID forKey:self.showId];
        NSLog(@"Event ID: %@",newEvent.UUID);
    }
}

以及删除事件的代码:

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

NSError *err;
BOOL success = YES;

NSLog(@"Event ID: %@",[[NSUserDefaults standardUserDefaults] objectForKey:self.showId]);

EKEvent *existingEvent = [eventStore eventWithIdentifier:[[NSUserDefaults standardUserDefaults] objectForKey:self.showId]];
NSLog(@"Existing event: %@",existingEvent);
if (existingEvent != nil) {
    success = [eventStore removeEvent:existingEvent span:EKSpanThisEvent error:&err];
}
if (success) {
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:self.showId];
}

为什么我无法从具有相同事件 ID 的日历中删除之前添加的事件?

此代码已在 iOS 5(iPad 1)和 iOS 6(新 iPad)上测试...

4

2 回答 2

7

我正在使用newEvent.eventIdentifier而不是newEvent.calendarItemIdentifier到目前为止,使用[store eventWithIdentifier:_project.event_identifier],我可以检索、删除和编辑现有事件。你应该试试。

于 2013-04-17T20:28:11.380 回答
1

文档警告说:“如果事件的日历发生变化,它的标识符很可能也会发生变化。” 在您存储 eventIdentifer 和尝试删除它之间,该事件是否有任何变化?

于 2012-10-10T14:03:07.433 回答