SAHM
SAHM

Reputation: 4179

Accessing programmatically created calendar on iOS device

I have figured out how to add a custom calendar within my iPhone app, but I can't figure out how to add that calendar to the device's Calendar. The event shows the correct calendar when viewed from within the app, but even when I tap on the calendar row for an event, my calendar is not in the list, and I have to cancel if I want to keep the calendar name correct. Is there a way to 'subscribe' to my locally created calendar so that I can see it from within the Calendar app on the device (obviously, I would also want it to sync with the user's computer, etc).

Upvotes: 2

Views: 4490

Answers (2)

Ben Hutchinson
Ben Hutchinson

Reputation: 221

I've been struggling with this today and it all seems down to what Calendar settings I (and ultimately the end user) has on the device.

Personally I sync my GMail Calendars down in Exchange format - doing so prevents the "local" calendars from showing up.

By manipulating the EKSource dependant on the user's settings should display your calendar correctly - this works for my Exchange scenario see the EKSource Class reference for more scenarios. Consider checking for iCloud!

EKCalendar *calendar = [EKCalendar calendarWithEventStore:self.eventStore];
calendar.title = @"My Calendar";

EKSource *localSource = nil;
EKSource *defaultSource = [self.eventStore defaultCalendarForNewEvents].source;

if (defaultSource.sourceType == EKSourceTypeExchange) {
    localSource = defaultSource;
} else {

    for (EKSource *source in self.eventStore.sources) {
        if (source.sourceType == EKSourceTypeLocal) {
            localSource = source;
            break;
        }
    }

}

if (localSource) {
    calendar.source = localSource;
} else {
    NSLog(@"Error: no local sources available");
}

NSError *error = nil;
BOOL result = [self.eventStore saveCalendar:calendar commit:YES error:&error];

if (result) {

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:calendar.calendarIdentifier forKey:@"Calendar"];
    [userDefaults synchronize];

    NSLog(@"Saved calendar to event store");

} else {
    NSLog(@"Error saving Calendar");
}

Upvotes: 1

SAHM
SAHM

Reputation: 4179

It has to do with iCloud. When iCloud is on, a calendar with a source that is local (EKSourceTypeLocal) will be hidden.

Upvotes: 6

Related Questions