在iPhone中使用Event Kit删除事件

我在iOS应用程序中使用Event Kit,并使用Event Kit创build事件。 我能够创build它,但我想给予删除的能力。 但我无法做到这一点。 我知道有一个EKEventStore删除事件的方法,但我不能使事件对象。 我有事件标识符作为一个string,但我不能创build一个事件对象使用它。 有人可以指导我做吗?

关于Pankaj

当你创build一个事件保存它的id像这样:

 NSString* str = [[NSString alloc] initWithFormat:@"%@", event.eventIdentifier]; 

通过id来删除事件,这里是代码

 EKEventStore* store = [[EKEventStore alloc] init]; EKEvent* event2 = [store eventWithIdentifier:str]; if (event2 != nil) { NSError* error = nil; [store removeEvent:event2 span:EKSpanThisEvent error:&error]; } 

请将此作为event.eventIdentifier更改值。 因此,您必须跟踪您设置到事件的event.title并访问该事件并将其删除

 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; } } 

在iOS上使用eventWithIdentifier来看看EKEvent

我也发现下面的代码工作

 // 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; } } 

这个代码的一个例子(未经testing):

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