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:
Instantiate an object of type
EKEventStore.Using the techniques that you learned earlier in this chapter, find the calendar object that you want to inspect and read from.
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.
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 ofEKEventStore.Pass the predicate created in step 4 to the
eventsMatchingPredicate:instance method ofEKEventStore. The result of this method is an array ofEKEventobjects (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=[[EKEventStorealloc]init];EKSource*icloudSource=[selfsourceInEventStore:eventStoresourceType:EKSourceTypeCalDAVsourceTitle:@"iCloud"];if(icloudSource==nil){NSLog(@"You have not configured iCloud for your device.");return;}EKCalendar*calendar=[selfcalendarWithTitle:@"Calendar"type:EKCalendarTypeCalDAVinSource:icloudSourceforEventType:EKEntityTypeEvent];if(calendar==nil){NSLog(@"Could not find the calendar we were looking for.");return;}/* The start ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access