16.8. Handling Event Changed Notifications

Problem

You want to get notified in your application when the user changes the contents of the Calendar database.

Solution

Register for the EKEventStoreChangedNotification notification:

- (EKCalendar *) 
    calDAVCalendarWithTitleContaining:(NSString *)paramDescription
                         inEventStore:(EKEventStore *)paramEventStore{
  
  EKCalendar *result = nil;
  
  for (EKCalendar *thisCalendar in paramEventStore.calendars){
    if (thisCalendar.type == EKCalendarTypeCalDAV){
      if ([thisCalendar.title 
           rangeOfString:paramDescription].location != NSNotFound){
        return thisCalendar;
      }
    }
  }
  
  return result;
  
}

- (void) eventsChanged:(NSNotification *)paramNotification{
  
  NSMutableArray *invalidatedEvents = [[NSMutableArray alloc] init];
  
  NSLog(@"Refreshing array of events...");
  
  for (EKEvent *event in self.eventsForOneYear){
    if ([event refresh] == NO){
      [invalidatedEvents addObject:event];
    }
  }
  
  if ([invalidatedEvents count] > 0){
    [self.eventsForOneYear removeObjectsInArray:invalidatedEvents];
  }
  
}

- (BOOL)            application:(UIApplication *)application
  didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
  
  self.eventStore = [[EKEventStore alloc] init];
  
  EKCalendar *calendar = 
  [self calDAVCalendarWithTitleContaining:@"vandad.np@gmail.com"
                             inEventStore:self.eventStore];
  
  NSTimeInterval NSOneYear = 1 * 365 * 24 * 60 * 60;
  
  NSDate *startDate = [NSDate date];
  NSDate *endDate = [startDate dateByAddingTimeInterval:NSOneYear];
  
  NSArray *calendars = [[NSArray alloc] initWithObjects:calendar, nil];
  
  NSPredicate ...

Get iOS 6 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.