EKEvent with eventWithIdentifier on iOS

11,008

Solution 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.

Solution 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?

Share:
11,008

Related videos on Youtube

Borut Tomazin
Author by

Borut Tomazin

iOS developer, technology enthusiast and devoted father.

Updated on September 15, 2022

Comments

  • Borut Tomazin
    Borut Tomazin almost 2 years

    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)...

  • Borut Tomazin
    Borut Tomazin over 11 years
    There is no change on calendar neither on event. I just create event and after after try to delete it with no luck and no error. It's really strange...
  • Borut Tomazin
    Borut Tomazin about 11 years
    That works. I don't actually understand what is calendarItemIdentifier for?
  • Mr_Nizzle
    Mr_Nizzle about 11 years
    Well calendarItemIdentifier is used when working with EKCalendarItem objects but here we're working with EKEvent object. - info: developer.apple.com/library/ios/#documentation/EventKit/…
  • Borut Tomazin
    Borut Tomazin about 11 years
    Thats why I thought so. calendarItemIdentifier is also iOS6+ specific. Thanks!
  • Mrug
    Mrug over 8 years
    Can anyone please help me for this : stackoverflow.com/questions/33931231/…