EventKit - App freezes when adding an EKEvent with 2 alarms (iOS 5) - iphone

I have an app that programmatically adds reminders to your iOS device's calendar.
Previous to iOS 5, I could add a calendar item with two alarms thusly:
EKEventStore* eventStore = [[EKEventStore alloc] init];
EKEvent* event = [EKEvent eventWithEventStore:eventStore];
// set startDate, endDate, title, location, etc.
[event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -5.0f]]; // 5 min
[event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -15.0f]]; // 15 min
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError* error = nil;
BOOL success = [eventStore saveEvent:event span:EKSpanThisEvent error:&error];
On iOS 5 this freezes the application. It does not return with an error - it just never returns.
If I only call addAlarm once, it works as expected.
On iOS 4.2, calling addAlarm twice works just fine.
Am I doing something wrong?

Its a bug with Apple. If you set 2 alarms it causes the app to freeze. If you only set 1 it works just fine. This is fixed in iOS 5.1 .

If you take a look at the EventKit section in the iOS 5 changes from iOS 4.3 document, it mentions that some items are deprecated for EKEvent. The hierarchy has changed and a new abstract superclass has been added: EKCalendarItem.

have you tried calling addAlarm using a variable?
EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:60.0f * -5.0f]]; // 5 min
[event addAlarm:alarm];
EKAlarm *alarm2 = [EKAlarm alarmWithRelativeOffset:60.0f * -15.0f]]; // 15 min
[event addAlarm:alarm2];

I had the same error.
The problem seems that startDate shoudln't be the same as endDate... really silly iOS change!

Related

How i can access Calendar Event and Schedular Event in our app? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to add events in iPhone using Event Kit framework
Adding particular date in default calender as an event
I am making an application in which i wanna use existing calendar events and schedular event in my app in iPhone.
and i also want to edit events through my app..
Thanks in advance...
Import EventKitUI/EventKitUI.h, EventKit/EventKit.h frameworks in your header file. This is the code to add an event to Default iPhone calendar
-(IBAction) addEvent:(id)sender
{
EKEventStore *eventStore = [[[EKEventStore alloc] init] autorelease];
EKEvent *events = [EKEvent eventWithEventStore:eventStore];
events.title = #"Title";
events.notes = #"Description";
events.location = #"Location";
events.startDate = [NSDate date];
events.endDate = [NSDate date];
events.availability = EKEventAvailabilityFree;
[events setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:events span:EKSpanThisEvent error:&err];
NSLog(#"Error From iCal : %#", [err description]);
}
Then to view the Events you can use EKEventEditViewController
EKEventEditViewController *editViewController = [[EKEventEditViewController alloc] init];
editViewController.editViewDelegate = self;
editViewController.event = event3;
editViewController.eventStore = eventStore1;
[self presentModalViewController:editViewController animated:YES];
Hope this helps
Refer EventKit framework to accomplish these.
The apple documentation is here.

how to set time in alarm using Eventkit framework in ios 6

I want to add events to calendar with alarm. If any event time is at 9.00 then alarm must be set at 8.45 ,How to add alarm time using EKAlarm.
Thanks in advance.
i just found a description at techotopia For set time in alarm using Eventkit framework in ios 6 like:-
-(void)createReminder
{
EKReminder *reminder = [EKReminder
reminderWithEventStore:self.eventStore];
reminder.title = _reminderText.text;
reminder.calendar = [_eventStore defaultCalendarForNewReminders];
NSDate *date = [_myDatePicker date];
EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:date];
[reminder addAlarm:alarm];
NSError *error = nil;
[_eventStore saveReminder:reminder commit:YES error:&error];
if (error)
NSLog(#"error = %#", error);
}
Hope its helps you :)

Adding particular date in default calender as an event

I am new in iPhone development.
There is a requirement in my application in which, there is a web service link which is below:
http://01s.in/webservices/sikhcalendar/getData.php?db_table=cal
so i want that the particular date which are shown in link, that date should be added in iCal which are add in my end in app. and it should generate an alert view on that particular day.
So, I am not getting how to add an event in iCal. Please give me some answer for this.
Thanks in advance.
adding a date in ical first you have to add the two framework in your code i.e.EventKit/EventKit.h, EventKitUI/EventKitUI.h and conforms the class to EKEventEditViewDelegate delegate and use the below method to add date in iCal
- (void)eventEditViewController:(EKEventEditViewController *)controller didCompleteWithAction:(EKEventEditViewAction)action
and i recommend you to go through this url and learn about these framework
Adding Event on the default calendar can be done using the following function
-(void)createEvent :(NSString *)eventTitle: (NSURL *)eventURL: (NSString *)eventNotes: (NSDate *)eventStartDate: (NSDate *)eventEndDate{
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = eventTitle;
event.URL = eventURL;
event.notes = eventNotes;
event.startDate = eventStartDate;
event.endDate = eventEndDate;
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
EKAlarm *myAlarm = [EKAlarm alarmWithRelativeOffset:0];
[event addAlarm:myAlarm];
NSError *err;
BOOL success = [eventStore saveEvent:event span:EKSpanThisEvent error:&err];
NSLog(#"event created success if value = 1 : %d", success);}
Here eventStartDate would be the time when the alarm which you set gets executed, and you get a notification
Please take a look into the EventKit framwork and the Apple Documentation.
Everything is there :)
Apple Documentation Calendar/Reminder

EKErrorDomain Code=1 in EventKit in ios5.0

I am getting following error in different places like when I am removing an event or when adding the an event.In that desccriptions it is showing that no calendar has been set but I debugged the event calendar and get the calendar is already set.I am confused.
Error Domain=EKErrorDomain Code=1 "No calendar has been set." UserInfo=0x756a8d0 {NSLocalizedDescription=No calendar has been set.}
Can any one suggest me if are there any chcekpoints or I am doing anything wrong?
Thanks in advance
I know the title of this question references iOS5, but I had an app running on iOS5 and greater. My iOS6 users were running into this issue for different reasons - you need to use the new iOS6 method if available to get access to the event store first.
- (void)requestAccessToEntityType:(EKEntityType)entityType completion:(EKEventStoreRequestAccessCompletionHandler)completion
Be sure to check availability of the API first, e.g.
EKEventStore* eventStore = [[EKEventStore alloc] init];
if([eventStore respondsToSelector:#selector(requestAccessToEntityType:completion:)]) {
// >= iOS 6
[eventStore requestAccessToEntityType:EKEntityTypeEvent
completion:^(BOOL granted, NSError *error) {
// may return on background thread
dispatch_async(dispatch_get_main_queue(), ^{
if (granted) {
// continue
} else {
// display error
}
});
}];
} else {
// < iOS 6
// continue
}
I figured it out, try this when creating a new EKEvent:
[event setCalendar:[eventStore defaultCalendarForNewEvents]];

Event Reminder time set, Off by 6 hours

I have searched for help on this issue but I am falling short of an answer. I am setting an event reminder with code. Using break points and stepping thru. I see the event time is correct. Below is how I set my reminder
EKEventStore *eventDB = [[[EKEventStore alloc] init]autorelease];
EKEvent *myEvent = [EKEvent eventWithEventStore:eventDB];
NSString * eventTitle = [NSString stringWithFormat:#"%# - %#",app.dealerBusinessName,serviceOrComments.text];
myEvent.title = eventTitle;
// "destinationDate" is the date I want to set the reminder for it is correct in debugger
//its format is 2011-06-03 15:45:58 +0000 which means (i would think) that the reminder
//should be set for 6/3/2011 3:45PM but its always 6 hours earlier (in this case at 9:45am
NSLog(#"value: %#",destinationDate);
myEvent.startDate = [[[NSDate alloc] initWithTimeInterval:0 sinceDate:destinationDate ]autorelease];
myEvent.endDate = [[[NSDate alloc] initWithTimeInterval:3600 sinceDate:myEvent.startDate]autorelease];
myEvent.allDay = NO;
myAlarmsArray = [[NSMutableArray alloc] init];
EKAlarm *alarm1 = [EKAlarm alarmWithRelativeOffset:-3600]; // 1 Hour
EKAlarm *alarm2 = [EKAlarm alarmWithRelativeOffset:-86400]; // 1 Day
[myAlarmsArray addObject:alarm1];
[myAlarmsArray addObject:alarm2];
myEvent.alarms = myAlarmsArray;
[myEvent setCalendar:[eventDB defaultCalendarForNewEvents]];
NSError *err;
[eventDB saveEvent:myEvent span:EKSpanThisEvent error:&err];
if (err == noErr) {
//no error, but do not show alert because we do that below.
}
Now im pretty sure I can just add 21,600 to the time but is that the correct way to do it? I am not understanding how NSDate works and its driving me nuts. Can someone explain to me what the correct way to do this is? Thanks!
I had a similar problem lately, it was really driving me nuts. You have to take your time zone into account (or the timezone your simulator/device are set to). NSDate represents always GMT time, NSCalendar the time within the time zone. So the time differences you are experiencing will exactly be the time difference between your time zone and GMT (your alarm gets triggered at 15:45 GMT).
Since I want my dates to be absolute, regardless of the time zone, I am setting all my NSCalendars to GMT - it works in my case, of course YMMV.