O'Reilly logo

iOS 7 Programming Cookbook by Vandad Nahavandipoor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

19.9. 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:

- (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];
    }

}

- (void) handleNotificationsInStore:(EKEventStore *)paramStore{

    EKSource *icloudSource = [self sourceInEventStore:paramStore
                                           sourceType:EKSourceTypeCalDAV
                                          sourceTitle:@"iCloud"];

    if (icloudSource == nil){
        NSLog(@"You have not configured iCloud for your device.");
        return;
    }

    EKCalendar *calendar = [self calendarWithTitle:@"Calendar"
                                              type:EKCalendarTypeCalDAV
                                          inSource:icloudSource
                                      forEventType:EKEntityTypeEvent];

    if (calendar == nil){
        NSLog(@"Could not find the calendar we were looking for.");
        return;
    }

    NSTimeInterval NSOneYear = 1 * 365 * 24 * 60 * 60;

    NSDate *startDate = [NSDate date];
    NSDate *endDate = [startDate dateByAddingTimeInterval:NSOneYear];

    NSPredicate *predicate =
    [paramStore predicateForEventsWithStartDate:startDate
                                        endDate:endDate
                                      calendars:@[calendar]];

    NSArray *events = [paramStore eventsMatchingPredicate:predicate];

    self.eventsForOneYear = [[NSMutableArray alloc ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required