Search code examples
xcode4

How can I write a program in Xcode that checks to see if a specific iCal calendar exists and access that calendar in order to add events?


I know that the EKEventStore class has the @property NSArray *calendars that returns an array of an event store's calendar objects, however I do not how to access a specific calendar inside of that array when all I know is the calendar's title. The only examples I can find online are of programs that access the defaultCalendarForNewEvents, but the calendar I want is not the default calendar. I also don't know the calendar's unique identifier, all I know is the title. I tried to use a valueForKey method to access the calendar titled BART, but what I am doing is definitely wrong, can anyone help? Here is what i tried:

@interface BARTClass : NSObject {

EKEventStore    *eventStore;

EKCalendar      *bart

NSArray         *calendars;

}

@property (nonatomic, retain) EKEventStore *eventStore;

@property (nonatomic, retain) NSArray *calendars;

@property (nonatomic, retain) EKCalendar *bart;

-(EKCalendar) getBartCalendar;

@end

...

@implementation

@synthesize calendars, bart, eventStore;

-(EKCalendar) getBartCalendar {

[self setEventStore: [[EKEventStore alloc] init]];

[self setCalendars = [eventStore calendars]];

NSArray *titles = [calendars valueForKey:@"title"];

[self setBart:[titles valueForKey:@"BART"]];

...


Solution

  • you are near to the goal, I think.

    You can do it like this:

    EKEventStore *eventStore = [[EKEventStore alloc]init];
    
    EKCalendar *bart;
    
    NSArray *myCals = [eventStore calendars];
    
    for (int i=0; i < [myCalls count]; i++) {
        bart = [myCals objectAtIndex:i];
        if (bart.title isEqualToString:@"Bart"){
           break;
        }
        else {
          bart = nil;
        }
    }
    

    If the calendar "Bart" exists, you get it at the end of the loop.