I am using Event Kit in my iOS application and creating a event using Event Kit. I am able to create it but I want to give the ability to delete also. But I am not able to do that. I know there is a method for EKEventStore to delete event but I am not able to make event object. I have event identifier as a string but I am not able to create an event object using it. Can someone please guide me to do it?
Regards Pankaj
Refer this as event.eventIdentifier changes value. So you must keep track of the event.title you set to the event and access the event and delete it
NSDate *startDate = <EVENT_START_DATE>;
NSDate *endDate = <EVENT_END_DATE>;
NSPredicate *predicateForEvents = [eventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:[NSArray arrayWithObject:[eventStore defaultCalendarForNewEvents]]];
//set predicate to search for an event of the calendar(you can set the startdate, enddate and check in the calendars other than the default Calendar)
NSArray *events_Array = [eventStore eventsMatchingPredicate: predicateForEvents];
//get array of events from the eventStore
for (EKEvent *eventToCheck in events_Array)
{
if( [eventToCheck.title isEqualToString: @"yourEventTitle"] )
{
NSError *err;
BOOL success = [eventStore removeEvent:eventToCheck span:EKSpanThisEvent error:&err];
NSLog( @"event deleted success if value = 1 : %d", success );
break;
}
}
When ever you are creating an event save its id like this:
NSString* str = [[NSString alloc] initWithFormat:@"%@", event.eventIdentifier];
pass the id to delete the event, here is code
EKEventStore* store = [[EKEventStore alloc] init];
EKEvent* event2 = [store eventWithIdentifier:str];
if (event2 != nil) {
NSError* error = nil;
[store removeEvent:event2 span:EKSpanThisEvent error:&error];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With