EKEvent with eventWithIdentifier on iOS

99封情书 提交于 2019-11-29 20:05:23

问题


If I want to retrieve EKEvent from EKEventStore with eventWithIdentifier method for previously saved event but I always get null.

This is the code for adding event:

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);
    }
}

And code for removing event:

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];
}

Why I cannot remove previously added event from calendar with the same event id ?

This code was tested on iOS 5 (iPad 1) and iOS 6 (new iPad)...


回答1:


I am using newEvent.eventIdentifier instead of newEvent.calendarItemIdentifier and so far, using [store eventWithIdentifier:_project.event_identifier], I can retrieve, delete and edit an existing event. you should try.




回答2:


The documentation warns that "If the calendar of an event changes, its identifier most likely changes as well." Did anything change about the event between the time you stored the eventIdentifer and when you attempted to delete it?



来源:https://stackoverflow.com/questions/12820501/ekevent-with-eventwithidentifier-on-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!