'EventKit remove event from calendar
This is the way I am adding all the events in to my calendar, those events are coming from table view. I have a problem with deleting a specific even from the calendar when the row on the table view gets deleted. The code that I am trying seems not to find and identifier in the calendar. Can you please let me know what I am missing here
ADD TO CALENDAR
let eventStore : EKEventStore = EKEventStore()
// 'EKEntityTypeReminder' or 'EKEntityTypeEvent'
eventStore.requestAccess(to: .event) { (granted, error) in
if (granted) && (error == nil) {
print("granted \(granted)")
print("error \(error)")
let event:EKEvent = EKEvent(eventStore: eventStore)
event.title = "Test Title"
event.startDate = Date()
event.endDate = Date()
event.notes = "This is a note"
event.calendar = eventStore.defaultCalendarForNewEvents
do {
try eventStore.save(event, span: .thisEvent)
} catch let error as NSError {
print("failed to save event with error : \(error)")
}
print("Saved Event")
}
else{
print("failed to save event with error : \(error) or access not granted")
}
}
DELETE FROM CALENDAR
func deleteEvent(_ storedEventID: String)
{
eventStore.requestAccess(to: .event, completion: { (granted, error) in
if (granted) && (error == nil)
{
if let calendarEvent_toDelete = self.eventStore.event(withIdentifier: storedEventID){
//recurring event
if calendarEvent_toDelete.recurrenceRules?.isEmpty == false
{
let alert = UIAlertController(title: "Repeating Event", message:
"This is a repeating event.", preferredStyle: UIAlertControllerStyle.alert)
//delete this event only
let thisEvent_Action = UIAlertAction(title: "Delete this event", style: UIAlertActionStyle.default)
{
(result : UIAlertAction) -> Void in
//sometimes doesn't delete anything, sometimes deletes all reccurent events, not just current!!!
do{
try self.eventStore.remove(calendarEvent_toDelete, span: .thisEvent)
} catch let e as NSError{return}
}
alert.addAction(thisEvent_Action)
}
//not recurring event
else{
//works fine
do{
try self.eventStore.remove(calendarEvent_toDelete, span: EKSpan.thisEvent)
} catch let e as NSError{
return
}
}
}
}
})
}
Solution 1:[1]
What I am missing in your example is to commit the changes to the event store.
Commit the changes immediately or with a separate commit while bulk processing multiple events.
try? self.eventStore.remove(eventToRemove, span: .thisEvent, commit: true)
Good luck and success.
Solution 2:[2]
first get the event using even Id then delete the event
func removeEvent(eventId: String, eventStore: EKEventStore) {
if let eventToDelete = self.eventStore.event(withIdentifier: eventId){
do {
try eventStore.remove(eventToDelete, span: .thisEvent)
} catch let error as NSError {
print("failed to save event with error : \(error)")
}
print("removed Event")
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Marc T. |
Solution 2 | Mallikarjun C |