In questo modo è possibile verificare se il calendario è già presente con titolo specifico. Se non esiste, è possibile crearlo in modo programmatico.
dichiarare un tipo booleano variabile
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;
}
}
// così ora verificare se la nostra variabile bool valore YES significa che un calendario con lo stesso nome/titolo già non exists.if quindi è possibile creare
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);
}
}
Come verificherebbe se il calendario esistesse già? Non vorrei creare il calendario più e più volte per caso? – Slee
Quando si crea il calendario, esiste una proprietà denominata calendarIdentifier che è un identificativo univoco. Memorizza quell'identificatore univoco (e anche l'identificatore dell'archivio eventi - assicurati che non sia cambiato) e controlla l'archivio degli eventi per vedere se esiste prima di crearlo. – kurtzmarc
Ma ecco il trucco: trovo che se la mia app crea un calendario locale, l'utente non è in grado di eliminarlo in seguito. Sembra un insetto. – matt