Question

I have a seemingly straightforward, textbook case of using EventKit to create events with alarms. Frustratingly, it doesn't work. Here's what I'm doing:

  1. Create an EKEvent with [EKEvent eventWithEventStore:] and fill out a bunch of its properties
  2. Add an alarm with [myEvent addAlarm:[EKAlarm alarmWithRelativeOffset:]]
  3. Save the event with [myEventStore saveEvent:span:error:]

The only "unusual" things about the whole process is that I'm using a phone with an older iOS version (iOS4.3.3 on an iPhone 4), and that all the calendars I'm using are synced to Google Calendars.

I have one calendar (let's call it CalendarA) that's set up in Google Calendars to have an automatic 30-minute alarm for new events, and another calendar (CalendarB) that's not set up for any automatic alarms.

Here's the broken behaviour I'm seeing:

  • When I add an EKAlarm with some sort of non-zero relativeOffset (e.g. 5 minutes) to CalendarA, the calendar ignores my offset and sets it to 30 minutes
  • When I do the same thing in CalendarB, my alarm gets ignored completely, and the event ends up having no alarm

What's really strange: If I set relativeOffset to zero, everything works perfectly well for that special case! (A zero-offset alarm is correctly added, both in the CalendarA and CalendarB cases). By the way, if I don't create an alarm at all, events in CalendarA still get a 30-minute alarm. I guess there's nothing that can be done about this.

I've also tried saving the event right after event creation, and then right away adding an alarm to the already-saved instance and saving it again. This didn't help.

If I manually create events with alarms through the native Calendar app on the phone, alarms work perfectly well, so I know that it is possible to sync arbitrary alarms to Google Calendars - the question is how to do it through code.

How can I make my event alarms behave correctly?

Was it helpful?

Solution 2

Wow, I just solved it... Apparently this had nothing to do with iOS versions, Google Calendars sync, etc. I just haven't realized that I need to pass a negative value to alarmWithRelativeOffset. This was completely non-intuitive. With a negative offset, it works perfectly. This also explains why zero offset was working before.

(I guess perhaps it might have a little bit to do with Google Calendars... I'm guessing that other calendars might support alarms after the event, and so that would have allowed me to debug the issue more easily, but it's not supported in Google Calendars so the invalid "alarm in the future" is just ignored).

There's still the minor issue where there's essentially no way to create an event without an alarm in CalendarA, but my guess is that nothing can be done here - even the native Calendar app has this issue.

OTHER TIPS

This worked for me on iOS 4.2, See if it helps you too

 EKEventStore* eventStore = [[EKEventStore alloc] init];
 EKEvent* event = [EKEvent eventWithEventStore:eventStore];
 // set startDate, endDate, title, location, etc.

[event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -5.0f]]; // 5 min

[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError* error = nil;
BOOL success = [eventStore saveEvent:event span:EKSpanThisEvent error:&error]; 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top