在iPhone中使用Event Kit删除事件

时间:2012-08-17 13:06:04

标签: iphone ios ekeventkit ekevent

我在iOS应用程序中使用Event Kit并使用Event Kit创建事件。我能够创建它,但我也希望能够删除它。但我无法做到这一点。我知道有一种方法可以让EKEventStore删除事件,但是我无法创建事件对象。我将事件标识符作为字符串,但我无法使用它创建事件对象。有人可以指导我这样做吗?

此致 的Pankaj

3 个答案:

答案 0 :(得分:8)

请参阅this,因为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;

    }
}

答案 1 :(得分:4)

当你创建一个事件时,保存它的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];
    } 

答案 2 :(得分:0)

查看EKEvent with eventWithIdentifier on iOS

我也发现以下代码可以正常工作

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

此代码的示例(未经测试):

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