EventKit doesn't work with app extensions

Originator:stephen.darlington
Number:rdar://19483608 Date Originated:15-Jan-2015
Status:Open Resolved:
Product:OS X SDK Product Version:10.10
Classification:Bug Reproducible:Yes
 
Summary:
I get permissions errors when I try to create an EKEventStore. 

    EKEventStore* store = [[EKEventStore alloc] init];

2015-01-15 14:05:37.858 Fail[6641:462049] CoreData: XPC: Unable to load metadata: Error Domain=NSCocoaErrorDomain Code=134070 "An error occurred in the persistent store." UserInfo=0x608000075600 {Problem=request failed, insufficient permission}
2015-01-15 14:05:37.859 Fail[6641:462049] CoreData: error: -addPersistentStoreWithType:NSXPCStore configuration:(null) URL:file:///Users/stephend/Library/Calendars/Calendar%20Cache options:{
    NSInferMappingModelAutomaticallyOption = 1;
    NSMigratePersistentStoresAutomaticallyOption = 1;
    agentOrDaemon = 1;
    serviceName = "com.apple.CalendarAgent.database";
} ... returned error Error Domain=NSCocoaErrorDomain Code=134070 "An error occurred in the persistent store." UserInfo=0x608000075600 {Problem=request failed, insufficient permission} with userInfo dictionary {
    Problem = "request failed, insufficient permission";
}

The documentation suggests that this should work.

Steps to Reproduce:
Project with the following line in a Today extension:

    EKEventStore* store = [[EKEventStore alloc] init];


Expected Results:
No errors. It works.

Actual Results:
An error. No ability to extract calendar entries.

Version:
Xcode 6.1.1, OS X 10.10.1

Notes:
Find attached sample project.

Configuration:
Macbook Pro 15", early 2011. 2.0 Ghz. 8Gb memory.

Attachments:
'EventKitBug.zip' was successfully uploaded.

Comments

I can confirm that event kit works with XPC as of 10.11, so I'd suggest testing this again with the newer API and see if the issue is resolved.

I didn't have to inherit sandbox in the service but I did have to add "com.apple.security.personal-information.calendars" entitlements to the XPC service. From there you get auth exactly the same way as you would from the main app.

Update 25-Jun-2015

It seems not to be fixed in El Cap I'm afraid. I have the following code:

    EKEventStore* store = [[EKEventStore alloc] init];

    [store requestAccessToEntityType:EKEntityTypeEvent
                          completion:^(BOOL granted, NSError* error) {
                              if (granted) {
                                  EKCalendar* cal = [store defaultCalendarForNewEvents];
                                  NSPredicate* predicate = [store predicateForEventsWithStartDate:[[NSDate date] wsl_beginningOfMonth]
                                                                                          endDate:[NSDate date]
                                                                                        calendars:@[ cal ]];
                                  NSArray* events = [store eventsMatchingPredicate:predicate];
                                  NSLog(@"Events: %@", events);
                              }
                              else {
                                  NSLog(@"Error: %@", error);
                              }
                          }];

If I run in a "normal" Mac app, I get a list of events from the default calendar.

When I run in a notification centre widget I get "NSDebugDescription=The connection to service named com.apple.CalendarAgent.proxy was invalidated." many times. (Find attached the log.) This happens when executing the first line.

Oddly, the store does get allocated and permission is granted -- i.e., the callback on the request permission method is called with granted == YES -- however, defaultCalendatForNewEvents returns nil. 'console.log' was successfully uploaded.

By stephen.darlington at June 25, 2015, 9:43 a.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!