Search code examples
iosobjective-cnscalendarekevent

How to delete old events from calendar in IOS


In my app I am saving events to calendar..

But If the event is from 1 Feb, 2015 to 20 Feb, 2015.

How can I delete the Events from 1 Feb, 2015 to 15 Feb, 2015 as those are completed events.

I googled and found the answer using Settings option of iPhone

https://apple.stackexchange.com/questions/103570/auto-delete-previous-old-events-from-ios-7-calendar

I use below code to delete all events of my calendar

NSDate *startDate = [NSDate date];
NSDate* endDate =  [NSDate dateWithTimeIntervalSinceNow:[[NSDate distantFuture] timeIntervalSinceReferenceDate]];
NSArray *calendarArray = [NSArray arrayWithObject:self.defaultCalendar];
NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate                                                                endDate:endDate                                                                        calendars:calendarArray];

NSArray *events = [self.eventStore eventsMatchingPredicate:predicate];
for (EKEvent *event in events)
{
  NSError* err = nil;
 [self.eventStore removeEvent:event span:EKSpanFutureEvents commit:YES error:&err];
}

But is any way of deleting old Events that are completed.Else delete Older than Feb, 2015 Events programmatically.

Note: I am using Recurrence Events that take 14 days if End date is not set..

Any ideas & suggestions how to fix it..

Thanks in Advance..!


Solution

  • I found the solution. Below code will delete all the events that are before past 2 days till 1 year back.

    Source Link:Fetching All Events

        NSDate *today = [NSDate date];
    
        NSDateComponents *components = [NSDateComponents new];
    
        [components setDay:-2];
    
        NSDate *endDate = [[NSCalendar currentCalendar] dateByAddingComponents:components
                                                                            toDate:today
                                                                           options:kNilOptions];
    
        NSDate* startDate =  [NSDate dateWithTimeIntervalSinceNow:[[NSDate distantPast] timeIntervalSinceReferenceDate]];
    
            // use Dictionary for remove duplicates produced by events covered more one year segment
        NSMutableDictionary *eventsDict = [NSMutableDictionary dictionaryWithCapacity:1024];
    
        NSDate* currentStart = [NSDate dateWithTimeInterval:0 sinceDate:startDate];
    
            int seconds_in_year = 60*60*24*365;
    
            // enumerate events by one year segment because iOS do not support predicate longer than 4 year !
            while ([currentStart compare:endDate] == NSOrderedAscending) {
    
        NSDate* currentFinish = [NSDate dateWithTimeInterval:seconds_in_year sinceDate:currentStart];
    
        NSArray *calendarArray = [NSArray arrayWithObject:self.defaultCalendar];
    
        if ([currentFinish compare:endDate] == NSOrderedDescending) {
                    currentFinish = [NSDate dateWithTimeInterval:0 sinceDate:endDate];
                }
        NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:currentStart endDate:currentFinish calendars:calendarArray];
        [self.eventStore enumerateEventsMatchingPredicate:predicate
                                                  usingBlock:^(EKEvent *event, BOOL *stop) {
    
                                                      if (event) {
                                                          [eventsDict setObject:event forKey:event.eventIdentifier];
                                                      }
    
                                                  }];
       currentStart = [NSDate dateWithTimeInterval:(seconds_in_year + 1) sinceDate:currentStart];
    
       }
    
      NSArray *events = [eventsDict allValues];
    
            for (EKEvent *event in events) {
                NSError* err = nil;
                [self.eventStore removeEvent:event span:EKSpanFutureEvents commit:YES error:&err];
    
            }
    

    Today date is 17 Feb, 2015. Above will delete all the events that are on or before 15 Feb, 2015.