mpemburn
mpemburn

Reputation: 2884

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

Upvotes: 11

Views: 7322

Answers (3)

Dave DeLong
Dave DeLong

Reputation: 243146

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];    

Upvotes: 21

n13
n13

Reputation: 6983

I get the list of calendars OK - the problem is I don't get a user-displayable list. The calendar.title property is null for all of them; I don't see any kind of id property either.

-> Update: It works now for me. The mistake I had made was to put the eventStore object in a temporary variable, then get the list of calendars, then release the eventStore. Well if you do that all your calendars go away too. Containment is not strictly object oriented in some of the iOS frameworks, and this is an example of that. That is, the calendar object is dependent on the event store, it's not its own, separate entity.

Anyway -the solution above is fine!

Upvotes: 2

mpemburn
mpemburn

Reputation: 2884

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

Upvotes: 7

Related Questions