October 2013
Intermediate to advanced
1053 pages
28h 7m
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:
-(void)addAlarmToCalendar:(EKCalendar*)paramCalendarinStore:(EKEventStore*)paramStore{/* The event starts 60 seconds from now */NSDate*startDate=[NSDatedateWithTimeIntervalSinceNow:60.0];/* And end the event 20 seconds after its start date */NSDate*endDate=[startDatedateByAddingTimeInterval:20.0];EKEvent*eventWithAlarm=[EKEventeventWithEventStore:paramStore];eventWithAlarm.calendar=paramCalendar;eventWithAlarm.startDate=startDate;eventWithAlarm.endDate=endDate;/* The alarm goes off 2 seconds before the event happens */EKAlarm*alarm=[EKAlarmalarmWithRelativeOffset:-2.0];eventWithAlarm.title=@"Event with Alarm";[eventWithAlarmaddAlarm:alarm];NSError*saveError=nil;if([paramStoresaveEvent:eventWithAlarmspan:EKSpanThisEventerror:&saveError]){NSLog(@"Saved an event that fires 60 seconds from now.");}else{NSLog(@"Failed to save the event. Error = %@",saveError);}}
For information about event stores and calendars and the way to retrieve instances to them, please see Recipe 19.2.
An event of type EKEvent can
have multiple alarms. Simply create the alarm using either the alarmWithAbsoluteDate: or alarmWithRelativeOffset: class method of
EKAlarm. The former ...
Read now
Unlock full access