Cover by Vandad Nahavandipoor

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

14.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 14.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 ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required