How to remove an existing Event from Apple Calendar

Hello


I am getting an error when I try to remove a saved event from Apple's Calendar app. (I am successfully saving the event to Apple's Calendar, but I am not able to remove the same event.)


In the custom class that subclasses NSObject :


import EventKit


inside the class’s init method :


eventStore = EKEventStore()


inside a function of this class :


let event = eventStore.eventWithIdentifier(savedCalendarItemIdentifier)
do {
  try eventStore.removeEvent(event, span: EKSpan.ThisEvent, commit: true)
} catch {
  /
}


savedCalendarItemIdentifier is taken from the original event the first time that it was saved to the calendar.


Here are the errors that I am getting.

Error getting event with identifier 8BCD8F3D-73D8-4E07-BAXX-69E05025D4F2: Error Domain=EKCADErrorDomain Code=1010 "(null)"

Error getting event with identifier 8BCD8F3D-73D8-4E07-BAXX-69E05025D4F2: Error Domain=EKCADErrorDomain Code=1010 "(null)"


Any ideas would be greatly appreciated!

Replies

savedCalendarItemIdentifier is taken from the original event the first time that it was saved to the calendar.

This may very probably the cause of your issue.


eventIdentifier

If the calendar of an event changes, its identifier most likely changes as well.

I'm not sure about what 'the calendar of an event changes' is meaning, but the description suggests that evenIdentifiers cannot be used as long-lasting identifier reliably.


In one of our apps which adds or removes some EKEvents, we embed a unique URL with custom scheme into each EKEvent and use it as an identifier.

There may be something persistent to identify EKEvents, but we could not have found one before our release date of the app, and the app seems to be working as expected till now.

Hello OOPer


Thank you very much for your quick and helpful response!


If you don't mind my asking, did you use the URL property of the EKCalendarItem Class to store a unque URL that identified each event? The URL value is visible to the user in the event details. If I need to associate my own persistent unique identifier with the event, then I prefer to hide this value from the user.


Thank you again.

The URL value is visible to the user in the event details.

That was one topic under discussion with our client. As for now, users can see the raw URL and we needed to convince our client with explaining that users would be able to open our app with tapping the URL...

Hello OOPer


Thank you again for your input. Using the URL value of the event worked well when tested. The only issue was that this value was editable by the end user, and if it was edited, then my app would lose the event.


I found your approach to loop through all events, looking for a specific identifier to be a good one, however. This worked better than trying to retrieve the event using the EKEventStore.eventWithIdentifier method (the original code, posted in my question).


Currently, I am using the EKCalendarItem’s calendarItemExternalIdentifier property to identify the events. (The specifications for this property seem to satisfy my requirements so far, e.g. I am developing an app for iOS only, etc.)


The approach that seems to be working now for me is to :

  1. Get all the events filtered by the event date.
  2. Find the specific event, by comparing the calendarItemExternalIdentifier against the value that I have saved.
  3. Update the event by creating a new one and removing the original one.


This seems to be a reliable method.


Thank you for putting me on the right path.

Thanks for reporting, an important fact that I have been missing. I wish we could re-design the functionality of our app, based on the reliable method, but our client would not give us enough time...