19.3. 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 19.1 and Recipe 19.2). 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
                      inCalendar:(EKCalendar *)paramCalendar
                    inEventStore:(EKEventStore *)paramStore
                           notes:(NSString *)paramNotes{

    BOOL result = NO;

    /* If a calendar does not allow modification of its contents
     then we cannot insert an event into it */
    if (paramCalendar.allowsContentModifications == NO){
        NSLog(@"The selected calendar does not allow modifications.");
        return NO;
    }

    /* Create an event */
    EKEvent *event = [EKEvent eventWithEventStore:paramStore];
    event.calendar = paramCalendar;

    /* Set the properties of the event such as its title,
     start date/time, end date/time, etc. */
    event.title = paramTitle;
    event.notes = paramNotes;
    event.startDate = paramStartDate;
    event.endDate = paramEndDate;

    /* Finally, save the event into the calendar */
    NSError *saveError = nil;

    result = [paramStore saveEvent:event
                              span:EKSpanThisEvent
                             error:&saveError];

    if (result == NO){
        NSLog(@"An error occurred = %@", saveError);
    }

    return result;

}

As you can see, this method expects a calendar object and ...

Get iOS 7 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.