代码之家  ›  专栏  ›  技术社区  ›  user2228755

将EKEvent发送到日历

  •  1
  • user2228755  · 技术社区  · 12 年前

    升级到Mavericks后,我的代码不再成功地将事件添加到日历中。通过Mavericks开发人员的发行说明,我没有发现与此问题相关的特定文档。

    你知道如何让这个代码正常工作吗?

    //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];
    
    1 回复  |  直到 12 年前
        1
  •  5
  •   ianyh    12 年前

    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,你必须进行一些版本检查,以决定是否需要请求权限。