16.2. Adding Events to Calendars
Problem
You would like to be able to create new events in users’ calendars.
Solution
Find the calendar you want to insert your event into (please refer
to Recipe 16.1). Create an
object of type EKEvent
using the
eventWithEventStore:
class method of
EKEvent
and save the event into the
user’s calendar using the saveEvent:span:error:
instance method of
EKEventStore
:
-
(
BOOL
)
createEventWithTitle:
(
NSString
*
)
paramTitle
startDate:
(
NSDate
*
)
paramStartDate
endDate:
(
NSDate
*
)
paramEndDate
inCalendarWithTitle:
(
NSString
*
)
paramCalendarTitle
inCalendarWithType:
(
EKCalendarType
)
paramCalendarType
notes:
(
NSString
*
)
paramNotes
{
BOOL
result
=
NO
;
EKEventStore
*
eventStore
=
[[
EKEventStore
alloc
]
init
];
/* Are there any calendars available to the event store? */
if
([
eventStore
.
calendars
count
]
==
0
){
NSLog
(
@"No calendars are found."
);
return
NO
;
}
EKCalendar
*
targetCalendar
=
nil
;
/* Try to find the calendar that the user asked for */
for
(
EKCalendar
*
thisCalendar
in
eventStore
.
calendars
){
if
([
thisCalendar
.
title
isEqualToString:
paramCalendarTitle
]
&&
thisCalendar
.
type
==
paramCalendarType
){
targetCalendar
=
thisCalendar
;
break
;
}
}
/* Make sure we found the calendar that we were asked to find */
if
(
targetCalendar
==
nil
){
NSLog
(
@"Could not find the requested calendar."
);
return
NO
;
}
/* If a calendar does not allow modification of its contents
then we cannot insert an event into it */
if
(
targetCalendar
.
allowsContentModifications
==
NO
){
NSLog
(
@"The selected 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.