xcode 以编程方式将事件添加到 ios 5 中的日历

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8141321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:26:29  来源:igfitidea点击:

Adding events to calendar in ios 5 programatically

xcodecalendarios5

提问by NaveenaRK

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];

The code above works fine in ios 4.2 but not in ios 5. I have the code in applicationDidfinishingLaunching method. Due to error, black screen appears and app exits. Only recurrenceRules has changed in ios 5 and I have not made use of it. All other properties are available in superclass EKCalendarItem. I cannot test it since I have xcode 3.2 and snow leopard. I am looking to debug the line at which error occurs causing the app to quit. I doubt it is related to setCalendar or using alarms property.

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

回答by NaveenaRK

The code is correct and works in iOS 5. The reason for my error was the first line

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

eventStore=[[EKEventStore alloc] init];

Since initializing eventstore takes some time, placing it in application launch method resulted in time out. I found it from my crash report stating:

由于初始化 eventstore 需要一些时间,将它放在应用程序启动方法中会导致超时。我从我的崩溃报告中发现它说:

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

The app is supposed to launch within 10 seconds. if not time out occurs with Exception Codes: 0x8badf00d

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

回答by Anton Sivov

You have to use 5th version of SDK. You can find a diff in saveEvent function:

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

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

It should help you.

它应该可以帮助你。

回答by craigmarch

There was a change (I believe) to the API in iOS5 that requires you to add EKAlarm objects using the addAlarminstance method.

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

To add an alarm to your event in iOS5:

在 iOS5 中为事件添加闹钟:

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

Check EKCalendarItem Class Referencefor details.

有关详细信息,请查看EKCalendarItem 类参考

Although @property(nonatomic, copy) NSArray *alarmsis not specified as read onlyit would appear to be behaving that way.

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

See https://stackoverflow.com/a/7880242/816455for more information on other iOS5 EKAlarm issues.

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

回答by KodeHyman

NaveenaRK I wasn't having any time out errors however i fixed this by doing the following.

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

You need to keep the eventStore in memory for the objects life time.

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

eventStore = [[EKEventStore alloc] init]

I initialised the event store on creating the object and released it in dealloc. The problem with setting the alarms and the "CADObjectGetInlineStringProperty" error were both fixed.

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