16.2. Adding Events to Calendars


You would like to be able to create new events in users’ calendars.


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
                           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;

  /* 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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.