views:

90

answers:

1

Hi

Is it possible to add events to iCal from my cocoa app? I tried using CalCalendarEvent but it didn't add anything to my calendar.

CalCalendarStore *calStore = [CalCalendarStore defaultCalendarStore];
CalEvent *event = [CalEvent event];

CalRecurrenceRule *recRule = [[CalRecurrenceRule alloc] initYearlyRecurrenceWithInterval:1 end:[CalRecurrenceEnd recurrenceEndWithOccurrenceCount:5]];
[event setRecurrenceRule:recRule];
[event setStartDate:currentDate];
[event setEndDate:endDate];
event.isAllDay = YES;

[calStore saveEvent:event span:CalSpanThisEvent error:NULL];

Thanks.

+3  A: 

I think you are missing a CalCalendar object.
A minimal CalEvent looks like:

CalEvent* event = [CalEvent event]; event.calendar = calendar; //this is important - otherwise the event does not appear in iCal event.title = title; event.startDate = startDate; event.endDate = endDate;

You could also check saveEvent's NSError.
Update: As Mike Abdullah points out in his comment, NSError should be handled with care.

weichsel
As ever, **don't** check the error object first. Check if the method returned `NO` and *then* look at the error object.
Mike Abdullah
added a link about that. thanks for your comment.
weichsel