Programmatically adding an event to calendar (iOS)

This was a little hard to track down, so I though I’d include the code to achieve this here.

Firstly, add the EventKit and EventKitUI frameworks to your project. Then these imports in your file of choice:

#import <EventKit/EventKit.h>
#import <EventKitUI/EventKitUI.h>

Next is the actual event creation code itself. I put this in a method invoked by target action from a UIBarButtonItem.

EKEventStore *eventStore = [[EKEventStore alloc] init];

// Initialise your event - perhaps from a pre-existing data structure EKEvent *calendarEvent = [EKEvent eventWithEventStore:eventStore]; calendarEvent.title = @"Your event title"; calendarEvent.startDate = [NSDate date]; // Your starting date here calendarEvent.endDate = [NSDate dateWithTimeIntervalSinceNow:600]; // Your end date here calendarEvent.notes = @"Details about your event here"; calendarEvent.location = @"Location of your event here"; [calendarEvent setCalendar:[eventStore defaultCalendarForNewEvents]]; // Initialise the event editing controller. There is also an event viewing controller that you can initialise, however // this gives the user the opportunity to check the details of the event before agreeing to it EKEventEditViewController *eventEditController = [[EKEventEditViewController alloc] init]; eventEditController.event = calendarEvent; eventEditController.editViewDelegate = self; eventEditController.eventStore = eventStore;
// Present the editing controller modally [self presentModalViewController:eventEditController animated:YES]; [eventEditController release]; [eventStore release];

To complete the process you also need to implement the EKEventEditViewDelegate in the object calling the event editing controller. The only @required delegate method to implement is:

- (void)eventEditViewController:(EKEventEditViewController *)controller didCompleteWithAction:(EKEventEditViewAction)action

The most basic implementation of this that functions is:

- (void)eventEditViewController:(EKEventEditViewController *)controller didCompleteWithAction:(EKEventEditViewAction)action {
    [self dismissModalViewControllerAnimated:YES];
}

Apple has a decent guide on this in the official documentation, although you could be forgiven for not finding it: http://developer.apple.com/library/ios/#documentation/DataManagement/Conceptual/EventKitProgGuide/Introduction/Introduction.html

Inaugural post for the JEON blog. This is what I&#8217;ve been looking at for most of today as I gradually learnt to bend the tumblr templating language to my will! I had been meaning to set this up for at least a couple of months now, so it&#8217;s fantastic to finally have done so.

Inaugural post for the JEON blog. This is what I’ve been looking at for most of today as I gradually learnt to bend the tumblr templating language to my will! I had been meaning to set this up for at least a couple of months now, so it’s fantastic to finally have done so.