Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can my app get a list of calendars on user's iPhone

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

like image 298
mpemburn Avatar asked Jan 08 '11 19:01

mpemburn


2 Answers

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];    
like image 137
Dave DeLong Avatar answered Oct 20 '22 04:10

Dave DeLong


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;
}
like image 38
mpemburn Avatar answered Oct 20 '22 06:10

mpemburn