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
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];
}
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;
}
}
Take a look at EKEvent with eventWithIdentifier on iOS
I'm also finding that the following code works
// eventWithIdentifier returns nil for the external identifier
//EKEvent *eventToRemove = [eventStore eventWithIdentifier:self.expiryCalendarExternalIdentifier];
// So I'm using this method, which seems to work.
- (EKEvent *) getEventForIdentifer: (NSString *) eventIdentifier {
EKEventStore *eventStore = [Settings getEventStore];
NSArray *items = [eventStore calendarItemsWithExternalIdentifier:eventIdentifier];
if ([items count] == 1) {
return items[0];
} else {
[LogFile write:@"getEventForIdentifer: more than one occurrence of event, or no event!"];
return nil;
}
}
An example of this code in action (untested):
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *newCalendarEvent = [EKEvent eventWithEventStore:eventStore];
// Just because we created an EKEvent, the calendarItemExternalIdentifier property of that event has been set.
// I store calendarItemExternalIdentifier (an NSString) in a Core Data
// object, and later fetch it back from core data.
// Something like:
NSManagedObjectSubclass *coreDataObject = ...
coreDataObject.externalId = newEvent.calendarItemExternalIdentifier;
// So, now let's say we pull the calendarItemExternalIdentifier string from
// Core Data, and want to remove the event.
NSString *externalId = ... // get calendarItemExternalIdentifier from Core Data
// getEventForIdentifer method from above
EKEvent *eventToRemove = [self getEventForIdentifer:externalId];
NSError *anError = nil;
[eventStore removeEvent:eventToRemove span:EKSpanThisEvent error:&anError];
if (anError) {
// Something has gone wrong. Report the error.
}
来源:https://stackoverflow.com/questions/12006421/deleting-an-event-using-event-kit-in-iphone