0

I have application that need to write events into native calendar. Writing works fine if added EKEvent doesnt contain EKAlarm. If i include this lines of code:

EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:-30];
event.alarms = [NSArray arrayWithObject:alarm];

Calendar item is not not added. It seems like execution of this line of code is not performed at all:

[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];

Ideas what can be the problem?

Marco
  • 6,692
  • 2
  • 27
  • 38
Ivan Alek
  • 1,899
  • 3
  • 19
  • 38

1 Answers1

5

I ran your code (and added what was necessary to make it complete) and had no problems. A solution to many hard-to-explain cases is to clean (build folder) and reset simulator/delete app from device. If you provide more code, it will be easier to find a solution. Btw, here is the code I used that was working:

- (void)addToCalendar {
    EKEventStore *eventStore = [[EKEventStore alloc] init];

    [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        EKEvent *event = [EKEvent eventWithEventStore:eventStore];
        EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:-30];
        event.alarms = [NSArray arrayWithObject:alarm];
        event.title = @"Title";

        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        event.startDate = [df dateFromString:@"2014-02-05 10:00:00"];
        event.endDate = [event.startDate dateByAddingTimeInterval:90*60];
        event.notes = @"Add some notes";

        [event setCalendar:[eventStore defaultCalendarForNewEvents]];

        NSError *err;
        [eventStore saveEvent:event span:EKSpanThisEvent error:&err];
    }];
}
Jorn
  • 1,054
  • 9
  • 25
  • Did you tried this code with multiple events with different start/end date? Because, i dont have problem when there is only one event, but with multiple events from once. – Ivan Alek Feb 04 '14 at 17:37
  • I added four events at three different times/dates (one duplicate to check that as well), all successful. What do you mean by "multiple events from once"? I added them by executing the code above (with modified dates) four times. Again, posting the complete code for the EKEvent might reveal a problem elsewhere. – Jorn Feb 04 '14 at 17:45