initWithAccessToEntityTypes:
在OS X 10.9中不推荐使用,因为OS X 10.9引入了与iOS 6中引入的安全功能类似的安全功能。也就是说,在OS X 10.9上,您必须申请使用EventKit API的权限,然后才能与事件进行实际交互。您可以使用以下方法
-[EKEventStore requestAccessToEntityType:completion:]
.
因此,您想要使用的代码看起来如下所示:
EKEventStore *eventStore = [[EKEventStore alloc] init];
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
// Event creation code here.
});
}];
对主队列的调度是因为事件存储完成回调可能发生在任意队列上。你可以阅读上面的文档
here
.
请注意
-[EKEventStore requestAccessToEntityType:完成时间:]
在OS X 10.9上才开始可用,所以如果你需要支持10.8,你必须进行一些版本检查,以决定是否需要请求权限。