Accessing programmatically created calendar on iOS device

限于喜欢 提交于 2019-12-01 00:37:09

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

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");
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!