如果我想使用EKEvent
方法从EKEventStore
检索先前保存的事件eventWithIdentifier
,但我总是为空。
这是添加事件的代码:
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)上测试...
答案 0 :(得分:7)
我使用newEvent.eventIdentifier
代替newEvent.calendarItemIdentifier
,到目前为止,使用[store eventWithIdentifier:_project.event_identifier]
,我可以检索,删除和编辑现有事件。你应该试试。
答案 1 :(得分:1)
文档警告说“如果事件的日历发生变化,其标识符很可能也会发生变化。”在您存储eventIdentifer和尝试删除它之间的事件有什么变化吗?