从日历中删除所有事件

时间:2017-05-09 14:32:43

标签: ios objective-c calendar

我正在创建一个iOS应用程序,它从服务器获取日期并将它们添加到日历中: 我正在使用此代码添加它们:

- (void)addReminderWithTitle:(NSString *)title date:(NSString *)date {
    NSString *dateString = date;
    NSDateFormatter *dateFormatter  = [NSDateFormatter new];
    [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    NSDate *dateFromString = [dateFormatter dateFromString:dateString];
    [dateFromString dateByAddingTimeInterval:-60*30];    
   EKEventStore *eventStore = [[EKEventStore alloc] init];
    [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        if (granted) {
        EKEvent *event = [EKEvent eventWithEventStore:eventStore];
        event.title = title;
        event.startDate = dateFromString;
            //[NSDate date]; //today
        event.endDate = [event.startDate dateByAddingTimeInterval:60*60];  //set 1 hour meeting
        event.calendar = [eventStore defaultCalendarForNewEvents];
        NSError *err = nil;
        [eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
        }}];
        }

我需要一个函数,它将删除用户特定操作的所有事件。 有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

您需要从日历中获取所有事件的数组,并通过某些标识符,日期或标题在数组中过滤它们,以便您可以获得自己添加的事件。

// Create the predicate from the event store's instance method
NSPredicate *predicate = [store predicateForEventsWithStartDate:oneDayAgo endDate:oneYearFromNow calendars:nil];

// Fetch all events that match the predicate
NSArray *events = [store eventsMatchingPredicate:predicate];

然后遍历events数组,并像这样删除它们

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