2

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

问候潘卡伊

4

3 回答 3

9

将此称为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;

    }
}
于 2012-08-25T06:11:19.550 回答
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];
    } 
于 2012-08-17T13:31:42.900 回答
0

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

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

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.
}
于 2014-02-08T23:29:03.447 回答