如果我想使用eventWithIdentifier方法从EKEventStore检索EKEvent以用于以前保存的事件,但我总是得到null.
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]; }
此代码在iOS 5(iPad 1)和iOS 6(新iPad)上进行了测试……
解决方法
我使用newEvent.eventIdentifier而不是newEvent.calendarItemIdentifier,到目前为止,使用[store eventWithIdentifier:_project.event_identifier],我可以检索,删除和编辑现有事件.你应该试试.