【问题标题】:Adding events to calendar in ios 5 programatically以编程方式将事件添加到 ios 5 中的日历
【发布时间】:2011-11-15 18:34:56
【问题描述】:
eventStore=[[EKEventStore alloc] init];
EKEvent *addEvent=[EKEvent eventWithEventStore:eventStore];
addEvent.title=@"hello";
addEvent.startDate=messageDate;
addEvent.endDate=[addEvent.startDate dateByAddingTimeInterval:600];
[addEvent setCalendar:[eventStore defaultCalendarForNewEvents]];
addEvent.alarms=[NSArray arrayWithObject:[EKAlarm alarmWithAbsoluteDate:addEvent.startDate]];
[eventStore saveEvent:addEvent span:EKSpanThisEvent error:nil];

上面的代码在 ios 4.2 中运行良好,但在 ios 5 中不行。我在 applicationDidfinishingLaunching 方法中有代码。由于错误,出现黑屏并退出应用程序。只有recurrenceRules 在ios 5 中发生了变化,我没有使用它。所有其他属性都在超类 EKCalendarItem 中可用。我无法测试它,因为我有 xcode 3.2 和雪豹。我正在寻找调试导致应用程序退出的错误发生的行。我怀疑它与 setCalendar 或使用警报属性有关。

【问题讨论】:

  • 它工作完美:)

标签: xcode calendar ios5


【解决方案1】:

代码正确且在 iOS 5 中有效。我的错误原因是第一行

eventStore=[[EKEventStore alloc] init];

由于初始化 eventstore 需要一些时间,因此将其置于应用程序启动方法会导致超时。我从我的崩溃报告中找到它:

"Elapsed application CPU time (seconds):30 seconds"

应用应该在 10 秒内启动。如果没有超时发生在Exception Codes: 0x8badf00d

【讨论】:

    【解决方案2】:

    您必须使用第 5 版 SDK。您可以在 saveEvent 函数中找到差异:

    [eventStore saveEvent:addEvent span:EKSpanThisEvent commit:YES error:nil]; 
    

    它应该对你有帮助。

    【讨论】:

      【解决方案3】:

      iOS5 中的 API 发生了变化(我相信),要求您使用 addAlarm 实例方法添加 EKAlarm 对象。

      在 iOS5 中为您的事件添加警报:

      [addEvent addAlarm:[EKAlarm alarmWithAbsoluteDate:addEvent.startDate]]
      

      详情请查看EKCalendarItem Class Reference

      虽然@property(nonatomic, copy) NSArray *alarms 没有被指定为只读,但它的行为似乎是这样的。

      有关其他 iOS5 EKAlarm 问题的更多信息,请参阅https://stackoverflow.com/a/7880242/816455

      【讨论】:

        【解决方案4】:

        NaveenaRK 我没有遇到任何超时错误,但是我通过执行以下操作解决了这个问题。

        您需要在对象生命周期内将 eventStore 保存在内存中。

        eventStore = [[EKEventStore alloc] init]
        

        我在创建对象时初始化了事件存储,并在 dealloc 中释放了它。设置警报的问题和“CADObjectGetInlineStringProperty”错误均已修复。

        【讨论】:

          猜你喜欢
          • 2017-09-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-09-19
          相关资源
          最近更新 更多