Accessing programmatically created calendar on iOS device

瘦欲@ 提交于 2019-11-30 19:14:30

问题


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).


回答1:


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




回答2:


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");
}


来源:https://stackoverflow.com/questions/9932430/accessing-programmatically-created-calendar-on-ios-device

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