将事件添加到自定义日历会导致应用程序崩溃

时间:2015-05-26 09:36:20

标签: ios objective-c calendar eventkit

我正在尝试将事件添加到自定义日历中。

要获取我在下面使用的日历列表

EKEventStore *eventStore = [[EKEventStore alloc] init];
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
   if(!granted) {
     //error alert
     return;
   }
   NSArray *calendars = [eventStore calendarsForEntityType:EKEntityTypeEvent];
   // List of all calendars
   // Let user to choose a calendar.
}];

为了添加一个事件,我使用了下面的代码

-(void)addEvent:(EventStoreClass *)storeObject
{
    EKEventStore *store = [[EKEventStore alloc] init];

    [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        if (!granted) return;

        EKEvent *event = [EKEvent eventWithEventStore:store];
        [event setTitle:[storeObject eventTitle]];
        [event setStartDate:[storeObject eventStartDate]];
        [event setEndDate:[storeObject eventEndDate]];
        [event setCalendar:[storeObject calendar] ? [storeObject calendar] : [store defaultCalendarForNewEvents]];
        [store saveEvent:event span:EKSpanThisEvent commit:YES error:nil];
    }];
}

现在,当我使用默认日历时,它已成功添加但当我选择任何其他日历时,应用程序崩溃并在日志中显示

  

*** - [EKSource retain]:发送到解除分配的实例0x7f9bddb1bf50的消息

enter image description here

我无法找出确切的问题或出现此问题的哪一行。任何建议都会很明显。

3 个答案:

答案 0 :(得分:0)

您必须检查特定日历是否允许您修改

  if ([storeObject calendar].allowsContentModifications)
    {
      //add event now  
    }

答案 1 :(得分:0)

是。在完成块开始运行之前,您的商店对象将被释放。

最简单的解决方法 - 将商店对象保存为字段:

EKEventStore *store;

-(void)addEvent:(EventStoreClass *)storeObject
{
   store = [[EKEventStore alloc] init];

    [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        if (!granted) return;

        EKEvent *event = [EKEvent eventWithEventStore:store];
        [event setTitle:[storeObject eventTitle]];
        [event setStartDate:[storeObject eventStartDate]];
        [event setEndDate:[storeObject eventEndDate]];
        [event setCalendar:[storeObject calendar] ? [storeObject calendar] : [store defaultCalendarForNewEvents]];
        [store saveEvent:event span:EKSpanThisEvent commit:YES error:nil];
    }];
}

答案 2 :(得分:0)

通过以下方式解决了这个问题。

-(void)addEvent:(EventStoreClass *)storeObject
{
    EKEventStore *store = [[EKEventStore alloc] init];

    [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        if (!granted) return;

        EKEvent *event = [EKEvent eventWithEventStore:store];
        [event setTitle:[storeObject eventTitle]];
        [event setStartDate:[storeObject eventStartDate]];
        [event setEndDate:[storeObject eventEndDate]];

        if([storeObject calendar]) {
            NSArray *calendars = [store calendarsForEntityType:EKEntityTypeEvent];
            __block EKCalendar *calendar;

            [calendars enumerateObjectsUsingBlock:^(EKCalendar *cal, NSUInteger idx, BOOL *stop) {
                if([[cal title] isEqualToString:[[storeObject calendar] title]]) {
                    calendar = cal;
                    *stop = YES;
                }
            }];

            [event setCalendar:calendar];
        }
        else {
            [event setCalendar:[store defaultCalendarForNewEvents]];
        }
        [store saveEvent:event span:EKSpanThisEvent commit:YES error:nil];
    }];
}

问题发生在[storeObject calendar],因为此日历是从另一家商店获取的,我们正在为其分配不同的商店。所以我只是从同一家商店拿走日历,这是有效的。