I want to add events in "someName" calendar. In case, a calendar with the given name doesn't exist then I will create one programatically. My problem is that the event does not get added when localSource (of type EKSource) turns out to be null. I added 3 checks to make sure that I get a value of localSource but even then in some cases localSource is nil. So on my phone events get added but on my friend's phone they don't.
I followed various posts and I understood that EKSource can be of 6 types: https://developer.apple.com/reference/eventkit/eksourcetype
What I fail to understand is in what cases localSource would be nil? What does this mean in normal language? Can I do something from the code to make it non-nil or something has to be done by the user on device?
- (void)setCalendar {
NSArray *calendars = [self.eventStore calendarsForEntityType:nil];
NSString *calendarTitle = someName;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title matches %@", calendarTitle];
NSArray *filtered = [calendars filteredArrayUsingPredicate:predicate];
if ([filtered count]) {
self.calendar = [filtered firstObject];
}
else {
self.calendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:self.eventStore];
self.calendar.title = calendarTitle;
EKSource *localSource;
for (EKSource *source in self.eventStore.sources)
{
//if iCloud account is setup then add the event in that calendar
if (source.sourceType == EKSourceTypeCalDAV && [source.title isEqualToString:@"iCloud"])
{
localSource = source;
break;
}
}
if (localSource == nil)
{
for (EKSource *source in self.eventStore.sources)
{
//if iCloud is not setup then look for local source
if (source.sourceType == EKSourceTypeLocal)
{
localSource = source;
break;
}
}
}
if (!localSource) {
localSource = [self.eventStore defaultCalendarForNewEvents].source;
}
self.calendar.source = localSource;
NSError *calendarErr = nil;
BOOL calendarSuccess = [self.eventStore saveCalendar:self.calendar commit:YES error:&calendarErr];
if (!calendarSuccess) {
NSLog(@"Error while updating calendar %@", calendarErr);
}
}
}
PS: I have permission to add calendar events.