February 2012
Intermediate to advanced
872 pages
22h 43m
English
You want to add alarms to the events in a calendar.
Use the alarmWithRelativeOffset: class method of
EKAlarm to create an instance of
EKAlarm. Add the alarm to an event
using the addAlarm: instance
method of EKEvent, like so:
- (EKCalendar *) getFirstModifiableLocalCalendar{ EKCalendar *result = nil; EKEventStore *eventStore = [[EKEventStore alloc] init]; for (EKCalendar *thisCalendar in eventStore.calendars){ if (thisCalendar.type == EKCalendarTypeLocal && [thisCalendar allowsContentModifications]){ return thisCalendar; } } return result; } - (void) addAlarmToCalendar{ EKCalendar *targetCalendar = [self getFirstModifiableLocalCalendar]; if (targetCalendar == nil){ NSLog(@"Could not find the target calendar."); return; } EKEventStore *eventStore = [[EKEventStore alloc] init]; /* The event starts 60 seconds from now */ NSDate *startDate = [NSDate dateWithTimeIntervalSinceNow:60.0f]; /* And end the event 20 seconds after its start date */ NSDate *endDate = [startDate dateByAddingTimeInterval:20.0f]; EKEvent *eventWithAlarm = [EKEvent eventWithEventStore:eventStore]; eventWithAlarm.calendar = targetCalendar; eventWithAlarm.startDate = startDate; eventWithAlarm.endDate = endDate; /* The alarm goes off two seconds before the event happens */ EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:-2.0f]; eventWithAlarm.title = @"Event with Alarm"; [eventWithAlarm addAlarm:alarm]; NSError *saveError = nil; if ([eventStore saveEvent:eventWithAlarm ...Read now
Unlock full access