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.4. Accessing the Contents of Calendars

Problem

You want to retrieve events of type EKEvent from a calendar of type EKCalendar on an iOS device.

Solution

Follow these steps:

  1. Instantiate an object of type EKEventStore.

  2. Using the techniques that you learned earlier in this chapter, find the calendar object that you want to inspect and read from.

  3. Determine the time and date where you want to start the search in the calendar and the time and date where the search must stop.

  4. Pass the calendar object (found in step 2), along with the two dates you found in step 3, to the predicateForEventsWithStartDate:endDate:calendars: instance method of EKEventStore.

  5. Pass the predicate created in step 4 to the eventsMatchingPredicate: instance method of EKEventStore. The result of this method is an array of EKEvent objects (if any) that fell between the given dates (step 3) in the specified calendar (step 2).

This code illustrates the preceding steps:

- (void) readEvents{

    /* Instantiate the event store */
    EKEventStore *eventStore = [[EKEventStore alloc] init];

    EKSource *icloudSource = [self sourceInEventStore:eventStore
                                           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;
    }

    /* The start ...

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