删除使用事件的套件,iPhone的事件(Deleting an event using Event

2019-09-21 07:20发布

我使用的事件包在我的iOS应用程序并创建使用事件Kit中的一个事件。 我能创建它,但我想给也能够进行删除操作。 但我不能够做到这一点。 我知道有对EKEventStore删除事件,但我不能够让事件对象的方法。 我有事件标识符作为一个字符串,但我不能够创建使用它的一个事件对象。 是否有人可以指导我做吗?

潘卡问候

Answer 1:

当过您要创建一个事件保存它的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];
    } 


Answer 2:

请参阅此作为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;

    }
}


Answer 3:

看看EKEvent与iOS上eventWithIdentifier

我还发现,下面的代码工作

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


文章来源: Deleting an event using Event Kit in iPhone