Question

After upgrading to Mavericks the my code no longer successfully adds events to the calendar. I found no specific documentation related to this issue through the Mavericks developer release notes.

Do you know how to get get this code working?

//Send new event to the calendar
NSString          *calEventID;

EKEventStore      *calStore = [[EKEventStore alloc]initWithAccessToEntityTypes:EKEntityTypeEvent];
EKEvent           *calEvent = [EKEvent eventWithEventStore:calStore];


//Calendar Values

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];


calEvent.title     = @"TITLE";
calEvent.startDate = [NSDate date];
calEvent.endDate   = [NSDate date];
calEvent.notes     = @"Here are some notes";


[calEvent setCalendar:[calStore defaultCalendarForNewEvents]];
calEventID = [calEvent eventIdentifier];


 NSError *error = nil;
[calStore saveEvent:calEvent span:EKSpanThisEvent commit:YES error:&error];
[calStore commit:nil];
Was it helpful?

Solution

initWithAccessToEntityTypes: is deprecated in OS X 10.9 because OS X 10.9 introduced security features similar to those introduced in iOS 6. That is, on OS X 10.9 you have to request permission to use the EventKit APIs before you can actually interact with the events. You do so by using the method -[EKEventStore requestAccessToEntityType:completion:].

So the code you would want to use would look something like:

EKEventStore *eventStore = [[EKEventStore alloc] init];
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
    dispatch_async(dispatch_get_main_queue(), ^{
        // Event creation code here.
    });
}];

The dispatch to the main queue is because the event store completion callback can happen on an arbitrary queue. You can read the docs on it here.

Note that -[EKEventStore requestAccessToEntityType:completion:] only started being available on OS X 10.9, so if you need to support 10.8 you're going to have do some version checking to decide whether or not you need to request permissions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top