I'm writing an iPhone app that will use the EventKit framework to create new events in the user's Calendar. That part works pretty well (except for the wonky way it handles the timezone -- but that's another issue). What I can't figure out is how to get a list of the user's calendars so that they can choose which calendar to add the event to. I know that its an EKCalendar object but the docs don't show any way to get the whole collection.
Thanks in advance,
Mark
Searching through the documentation reveals an EKEventStore
class that has a calendars
property.
My guess is that you'd do something like:
EKEventStore * eventStore = [[EKEventStore alloc] init];
NSArray * calendars = [eventStore calendars];
EDIT: As of iOS 6, you need to specify whether you want to retrieve calendars of reminders or calendars of events:
EKEventStore * eventStore = [[EKEventStore alloc] init];
EKEntityType type = // EKEntityTypeReminder or EKEntityTypeEvent
NSArray * calendars = [eventStore calendarsForEntityType:type];
The code I used to get a useable NSDictionary of calendar names and types is like this:
//*** Returns a dictionary containing device's calendars by type (only writable calendars)
- (NSDictionary *)listCalendars {
EKEventStore *eventDB = [[EKEventStore alloc] init];
NSArray * calendars = [eventDB calendars];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
NSString * typeString = @"";
for (EKCalendar *thisCalendar in calendars) {
EKCalendarType type = thisCalendar.type;
if (type == EKCalendarTypeLocal) {
typeString = @"local";
}
if (type == EKCalendarTypeCalDAV) {
typeString = @"calDAV";
}
if (type == EKCalendarTypeExchange) {
typeString = @"exchange";
}
if (type == EKCalendarTypeSubscription) {
typeString = @"subscription";
}
if (type == EKCalendarTypeBirthday) {
typeString = @"birthday";
}
if (thisCalendar.allowsContentModifications) {
NSLog(@"The title is:%@", thisCalendar.title);
[dict setObject: typeString forKey: thisCalendar.title];
}
}
return dict;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With