I'm trying to create an event with one alarm programmatically like that:
+(void)exportEvent:(AgendaEvent*)evento
onCalendar:(EKCalendar*)calendar {
EKEventStore* store= [[[EKEventStore alloc] init] autorelease];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if(!granted) {
// show "not granted" message
return;
}
// save event
NSCalendar* gc= [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
EKEvent* event= [EKEvent eventWithEventStore:store];
event.title= evento.descrizione;
event.startDate= [gc dateFromComponents:evento.begin];
if(evento.end)
event.endDate= [gc dateFromComponents:evento.end];
else {
NSDateComponents* endDateComponents= [[evento.begin copy] autorelease];
endDateComponents.day++;
endDateComponents.hour= 0;
endDateComponents.minute= -1;
endDateComponents.second= 0;
NSDate* endDate= [gc dateFromComponents:endDateComponents];
// endDate is correctly set at 23:59 of the same day of beginDate, when all day beginDay is at 00:00
event.endDate= endDate;
event.allDay= YES;
}
event.calendar= calendar;
// reminder
NSDateComponents* reminderDateComponents= [[evento.begin copy] autorelease];
reminderDateComponents.day--;
reminderDateComponents.hour= 9;
reminderDateComponents.minute= 0;
NSDate* reminderDate= [gc dateFromComponents:reminderDateComponents];
// reminder date is correctly set at 9:00 of the previous day of beginDate
[event addAlarm:[EKAlarm alarmWithAbsoluteDate:reminderDate]];
NSError* err= nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
if(err) {
// show "unable to export" message
return;
}
// show "exported" message
});
}];
}
but some times only (or more correctly often) [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err]
fails with:
2014-06-13 09:34:01.300 xxx[224:60b] CADObjectGetRelation failed with error Error Domain=NSMachErrorDomain Code=268435459 "The operation couldn’t be completed. (Mach error 268435459 - (ipc/send) invalid destination port)"
2014-06-13 09:34:01.301 xxx[224:60b] Impossibile esportare evento: Error Domain=EKErrorDomain Code=29 "Impossibile modificare avvisi." UserInfo=0x16a7c7e0 {NSLocalizedDescription=Impossibile modificare avvisi.}
I can't even find a description for code 29 in EKErrorDomain, does anybody have a clue?
Please mind that:
CADObjectGetRelation
related message is not always shown even if event creation fails but seems to not appear when the event and alarms are created.Ok after some trial and error I managed to get it working.
The problem is that just before calling exportEvent:
I was creating another EKEventStore
to read and select an EKCalendar
calendar.
I removed store
from the question selector body and passed it as a parameter from the previous step and now it works. This, I suppose, because there is some ipc involved and store
is deallocated between user calendar selection step and the actual event creation. If not enough time passes between deallocation in step1 and reallocation in step2 ipc connection gets denied which explains why sometimes it was working.