16.3. 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
calendars
property of the event store (instantiated in step 1), find the calendar you want to 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 ofEKEvent
objects (if any) that fell between the given dates (step 3) in the specified calendar (step 2).
This code illustrates the above steps:
-
(
EKCalendar
*
)
calDAVCalendarWithTitleContaining
:
(
NSString
*
)
paramDescription
{
EKCalendar
*
result
=
nil
;
EKEventStore
*
eventStore
=
[[
EKEventStore
alloc
]
init
];
for
(
EKCalendar
*
thisCalendar
in
eventStore
.
calendars
){
if
(
thisCalendar
.
type
==
EKCalendarTypeCalDAV
){
if
([
thisCalendar
.
title
rangeOfString:
paramDescription
].
location
!=
NSNotFound
){
return
thisCalendar
;
}
}
}
return
result
;
}
-
(
void
)
readEvents
{
/* Find a calendar to base our search on */
EKCalendar
*
targetCalendar
=
[
self
calDAVCalendarWithTitleContaining:
@"gmail.com"
];
/* If we could not find a CalDAV calendar ...
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.