Así es como se puede comprobar si un calendario ya existe con el título específico. Si no existe, puede crearlo programáticamente.
Declarar una variable booleana Tipo
BOOL doesExist=NO;
EKEventStore *eventStore=[[EKEventStore alloc] init];
NSArray *calanders=[eventStore calendarsForEntityType:EKEntityTypeEvent];
//Now Iterate through every calendar in the array and match its title
// with the title that you want to create
for(EKCalendar calendar in calendars)
{
if([[calendar title] isEqualToString:@"youdesiredname"])
{
doesExist=YES;
}
}
// por lo que ahora comprobar si nuestra variable bool contiene el valor SÍ significa que un calendario con el mismo nombre/título ya no exists.if entonces usted puede crear
if(!doesExist)
{
NSString* calendarName = @"DesiredCalendarName";
EKCalendar* calendar;
EKSource* localSource;
for (EKSource* source in eventStore.sources) {
if (source.sourceType == EKSourceTypeLocal)
{
localSource = source;
break;
}
if (!localSource)
return;
calendar = [EKCalendar calendarWithEventStore:eventStore];
calendar.source = localSource;
calendar.title = calendarName;
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
calendar = [eventStore calendarWithIdentifier:self.calendarIdentifier];
event.calendar = calendar;
// Set the start date to the current date/time and the event duration to one hour
NSDate *startDate = [NSDate date];
event.startDate = startDate;
event.endDate = [startDate dateByAddingTimeInterval:3600];
//And to save the event to the event database:
NSError *error = nil;
BOOL result = [eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&error];
if (result)
{
NSLog(@"Saved event to event store.")
}
else
{
NSLog(@"Error saving event: %@.", saveError);
}
NSError* error;
bool success= [eventStore saveCalendar:calendar commit:YES error:&error];
if (error != nil)
{
NSLog(error.description);
}
}
¿Cómo verificaría si el calendario ya existía? No me gustaría crear el calendario una y otra vez por accidente. – Slee
Cuando crea el calendario, hay una propiedad llamada calendarIdentifier que es un identificador único. Almacene ese identificador único (y el identificador de tienda de eventos también - asegúrese de que no haya cambiado) y verifique el almacén de eventos para ver si existe antes de crearlo. – kurtzmarc
Pero aquí está el problema: me parece que si mi aplicación crea un calendario local, el usuario no puede eliminarlo más tarde. Eso parece un error. – matt