O'Reilly logo

iOS 7 Programming Cookbook by Vandad Nahavandipoor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required