Deleting an event using Event Kit in iPhone

吃可爱长大的小学妹 提交于 2019-12-08 00:21:21

问题


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


回答1:


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



回答2:


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;

    }
}



回答3:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!