How to create and save EKCalendar on ios 6

2020-01-30 08:15发布

I'm having an issue where I create my EKCalendar and everything looks good but then when I go to list my calendars, it doesn't show up. I also go to check my calendar list in my calendar app but it is non existant. Any thoughts?

Here is my button code to create my calendar:

- (IBAction)one:(id)sender {
NSString* calendarName = @"My Cal";
EKCalendar* calendar;

// Get the calendar source
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;

NSError* error;
bool success= [eventStore saveCalendar:calendar commit:YES error:&error];
if (error != nil)
{
    NSLog(error.description);
    // TODO: error handling here
}
NSLog(@"cal id = %@", calendar.calendarIdentifier);
}

And here is my button code to list the calendar, but my new calendar is never included!

- (IBAction)two:(id)sender {

NSArray *calendars = [eventStore calendarsForEntityType:EKEntityTypeEvent];

for (EKCalendar* cal in calendars){
    NSLog(@"%@",cal.title);
}

}

Thank you in advance!

7条回答
时光不老,我们不散
2楼-- · 2020-01-30 08:40

I would not recommend the top answer, since it relies on checking for "iCloud" as the name, something which can be changed by the user. If you just want to make sure the calendar gets saved and you don't necessarily care what source it gets, you could do this:

EKCalendar *calendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:eventStore];
calendar.title = @"Calendar name";

NSError *calendarError = nil;
for (EKSource *source in eventStore.sources) {
    // We know the birthday source is read-only
    if (source.sourceType == EKSourceTypeBirthdays) {
        continue;
    }

    calendar.source = source;
    [eventStore saveCalendar:calendar commit:YES error:&calendarError];

    // If saving succeeded, we break, otherwise we try a new source
    if (!calendarError) {
        break;
    }
}
查看更多
登录 后发表回答