NaveenaRK
NaveenaRK

Reputation: 71

Adding events to calendar in ios 5 programatically

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.

Upvotes: 2

Views: 13411

Answers (4)

NaveenaRK
NaveenaRK

Reputation: 71

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

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:

"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

Upvotes: 3

Kodejack
Kodejack

Reputation: 441

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

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

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.

Upvotes: 0

craigmarch
craigmarch

Reputation: 534

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

To add an alarm to your event in iOS5:

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

Check EKCalendarItem Class Reference for details.

Although @property(nonatomic, copy) NSArray *alarms is not specified as read only it would appear to be behaving that way.

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

Upvotes: 0

Anton Sivov
Anton Sivov

Reputation: 414

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

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

It should help you.

Upvotes: 1

Related Questions