EKEventKit is a somewhat recent addition to the iPhone SDK, and allows an app to set, delete or edit items in the iPhone calendar.

This can be handy if you have an app that requires prompting the user for an action at a given time.

Recently Blue Sun Software completed and deployed an app for a Sydney based Optometrist, of which had a requirement to be able to set and maintain reminders to change contact lenses for customers.

First step is to bring across the imports. You will need to import 2 frameworks – EventKit, and the EventKitUI framework. EventKit gives you the methods necessary, and EventKitUI the view controllers to edit and delete.

Import these in your header file as such:

#import 
#import 
//Define these in your @interface declaration
EKEventStore *eventStore;
EKEventEditViewController *editController;

In order to create an event, there are a lot of options to take care of. Here is the code that creates an event for the app I built.

//pop this into your viewdidload
//Eventstore is the object that allows us to interact with the iPhone events
eventStore = [[EKEventStore alloc] init];
//EditViewController is the view controller that allows edit and delete of events
editController = [[EKEventEditViewController alloc]init];
// use this in the method that actually creates the event
NSError *err = nil;
//This date will be the date our reminder expires, as in stops recurring.  Two years was chosen as most users
//will replace their device after 2 years
NSDate *twoYearsFromNow = [NSDate dateWithTimeIntervalSinceNow:63113851];
//Define the recurrance rule
EKRecurrenceRule *recurrance;
NSDateComponents *comp = [[NSDateComponents alloc]init];
 
// The application actually allows multiple combinations of reminder periods and expiry for contacts - I've
// only included the 2 week reminder here for simplicity
[comp setYear:0];
[comp setMonth:0];
[comp setDay:14];
//Recurr every 2weeks
recurrance = [[EKRecurrenceRule alloc] initRecurrenceWithFrequency:EKRecurrenceFrequencyWeekly interval:2 end:[EKRecurrenceEnd recurrenceEndWithEndDate:twoYearsFromNow]];
 
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
//Our remind date
NSDate *eventDate = [cal dateByAddingComponents:comp toDate:[NSDate date] options:0];
 
//Create alarm 4 hours before they need to be changed (i.e. at 8pm)
double alarmAmountInSeconds = 60.0*60.0*4.0;
EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:(-1.0*alarmAmountInSeconds)];
 
//Create event
EKEvent *myEvent = [EKEvent eventWithEventStore:eventStore];
 
// These flags are pretty self explanatory
myEvent.calendar = eventStore.defaultCalendarForNewEvents;
myEvent.title = @"Change contact lenses";
myEvent.startDate = eventDate;
myEvent.endDate = eventDate;
myEvent.allDay = TRUE;
myEvent.location = @"Alan Wong Optical";
myEvent.recurrenceRule = recurrance;
 
//You can set multiple alarms if you wish, i.e. 1 alarm 10 minutes before, and another a day before if you wish
myEvent.alarms = [NSArray arrayWithObject:alarm];
myEvent.notes = @"Please change contact lenses";
 
// Try to save the event
BOOL result = [eventStore saveEvent:myEvent span:EKSpanFutureEvents error:&err];
 
if(result){
   NSLog(@"EVENT SAVED, %@",myEvent.eventIdentifier);
   [userDefaults setObject:myEvent.eventIdentifier forKey:@"changeContactsReminder"];
}
else{
   NSLog(@"SOMETHING BAD %@", err);
}

At the end of this code, an event will have been created and stored. This event will recur every 2 weeks for 2 years, and remind the user at 8pm the previous day to change their contacts. The event ID will be stored in the userdefault key ‘changeContactsReminder’.

If you have a need in the app to be able to edit or delete this event, you can do so by using the EventKitUI controller. A simple example of this is here:

//Set the eventsotre to our eventstore
editController.eventStore = eventStore;
editController.editViewDelegate = self;
//Set the event to the event ID saved from the code above (in my case in userdefaults)
editController.event = [eventStore eventWithIdentifier:[userDefaults objectForKey:@"changeContactsReminder"]];
[self.navigationController presentModalViewController:editController animated:YES];

Sorted!