Can't add/change alarms EKAlarm to a [newly created] EKEvent event - ios

I'm trying to create an event with one alarm programmatically like that:
+(void)exportEvent:(AgendaEvent*)evento
onCalendar:(EKCalendar*)calendar {
EKEventStore* store= [[[EKEventStore alloc] init] autorelease];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if(!granted) {
// show "not granted" message
return;
}
// save event
NSCalendar* gc= [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
EKEvent* event= [EKEvent eventWithEventStore:store];
event.title= evento.descrizione;
event.startDate= [gc dateFromComponents:evento.begin];
if(evento.end)
event.endDate= [gc dateFromComponents:evento.end];
else {
NSDateComponents* endDateComponents= [[evento.begin copy] autorelease];
endDateComponents.day++;
endDateComponents.hour= 0;
endDateComponents.minute= -1;
endDateComponents.second= 0;
NSDate* endDate= [gc dateFromComponents:endDateComponents];
// endDate is correctly set at 23:59 of the same day of beginDate, when all day beginDay is at 00:00
event.endDate= endDate;
event.allDay= YES;
}
event.calendar= calendar;
// reminder
NSDateComponents* reminderDateComponents= [[evento.begin copy] autorelease];
reminderDateComponents.day--;
reminderDateComponents.hour= 9;
reminderDateComponents.minute= 0;
NSDate* reminderDate= [gc dateFromComponents:reminderDateComponents];
// reminder date is correctly set at 9:00 of the previous day of beginDate
[event addAlarm:[EKAlarm alarmWithAbsoluteDate:reminderDate]];
NSError* err= nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
if(err) {
// show "unable to export" message
return;
}
// show "exported" message
});
}];
}
but some times only (or more correctly often) [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err] fails with:
2014-06-13 09:34:01.300 xxx[224:60b] CADObjectGetRelation failed with error Error Domain=NSMachErrorDomain Code=268435459 "The operation couldn’t be completed. (Mach error 268435459 - (ipc/send) invalid destination port)"
2014-06-13 09:34:01.301 xxx[224:60b] Impossibile esportare evento: Error Domain=EKErrorDomain Code=29 "Impossibile modificare avvisi." UserInfo=0x16a7c7e0 {NSLocalizedDescription=Impossibile modificare avvisi.}
I can't even find a description for code 29 in EKErrorDomain, does anybody have a clue?
Please mind that:
I'm not using arc as you can see but seems pretty correct to me (an to static analyzer too).
I've also tried to split the event save in two phases: one for the event and one for the alarm with exactly the same results.
"Impossibile modificare avvisi." means "Can not change alerts."
Tried on an iPad air with ios7.1.1 and ios7.1 simulator
CADObjectGetRelation related message is not always shown even if event creation fails but seems to not appear when the event and alarms are created.

Ok after some trial and error I managed to get it working.
The problem is that just before calling exportEvent: I was creating another EKEventStore to read and select an EKCalendar calendar.
I removed store from the question selector body and passed it as a parameter from the previous step and now it works. This, I suppose, because there is some ipc involved and store is deallocated between user calendar selection step and the actual event creation. If not enough time passes between deallocation in step1 and reallocation in step2 ipc connection gets denied which explains why sometimes it was working.

Related

Open "Event Details" in calendar app with specific event id

I am trying to open calendar with specific event, I have added events programmatically and all the IDs of these event are persistent.
This is how i add event
-(IBAction)addEvent:(id)sender{
EKEventStore *store = [[EKEventStore alloc]init];
[store requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error) {
if (!granted) { return; }
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = #"Demo Title";
NSDateComponents *comps=[[NSDateComponents alloc]init];
[comps setDay:4];
[comps setMonth:10];
[comps setYear:2016];
[comps setHour:12];
[comps setMinute:1];
NSDate *date=[[[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian] dateFromComponents:comps];
_date=date;
event.startDate = date;
event.endDate=date;
event.notes=#"This is event description";
EKAlarm *alarm=[EKAlarm alarmWithAbsoluteDate:date];
[event addAlarm:alarm];
event.calendar = [store defaultCalendarForNewEvents];
NSError *err=nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
}];
}
The event is added successfully and i'm also able to open calendar app but the thing is i can't navigate inside the event detail in calendar
here is how i open calendar
-(IBAction)openCalender:(id)sender{
NSInteger interval = [_date timeIntervalSinceReferenceDate];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"calshow:%ld?eventid=%#", (long)interval,_eventID]];
NSLog(#"%#",url);
[[UIApplication sharedApplication] openURL:url];
}
What i want is in below screenshot
What i'm getting is
any idea?
To answer my own question, this isn't possible with any of public API
I searched over a year but this is not possible with any public API, but i guess there might be some private API to do this (and probably doing that will get our app rejected).
Widget of calendar app is doing the same what i want to do but it's Apple's API wont allow us to use it. lol
I really don't think there's a way to do this. Even when creating a new calendar event from the default Mail app opens the calendar day view. If this functionality was possible I'm sure they would have at least implemented it in the default mail app.

unable to add event using EventKit in iOS app

I am trying to add event in iOS app using Event kit with following code:
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) { return; }
dispatch_async(dispatch_get_main_queue(), ^{
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = eventName;
event.startDate = [NSDate dateFromString:[NSString stringWithFormat:#"%# %#",[arrEvent objectAtIndex:1],[arrEvent objectAtIndex:2]] withFormat:#"MM/dd/yyyy HH:mm"];
event.endDate = [NSDate dateFromString:[NSString stringWithFormat:#"%# %#",[arrEvent objectAtIndex:1],[arrEvent objectAtIndex:3]] withFormat:#"MM/dd/yyyy HH:mm"];
[event setCalendar:[store defaultCalendarForNewEvents]];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
NSString *savedEventId = event.eventIdentifier; //this is so you can access this event later
NSLog(#"%#",savedEventId);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Event added to calendar" message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"View Calendar", nil];
alert.tag = 101;
[alert show];
});
}];
But I am getting granted as false so it doesn't add the event. It is not working in my simulator and device both, even after I reset my simulator. Same code works in another app of mine. Can someone please suggest me what I should do?
Edit: Error is always nil
When app launched for first time, a message asking for permissions will be shown to the user, and app will be able to access the calendar if only the user granted permission. In your case you have declined permission for calendar.
For fixing this issue, Go to Settings -> -> Allow to access calendar = YES.
Seems like you're requesting to get the access every time. You should better check the authorisation status first and if it's not determined, only then request for the authorisation. First check this using "authorizationStatusForEntityType" and create a new method to save the event, not inside "requestAccessToEntityType".

Can't delete single ekevent of recurrent event in calendar iOS

I've developed an app that uses calendar using the eventkit.
The user can push a button and delete a specific event. Everything works just fine, except when it comes to recurrent events.
If the user itself created the recurrent events, it works as expected, the single event is deleted and the rest remains in calendar. But if the recurrent events is created by other and accepted by user and then try to delete one specific of those events, they all disappear from calendar. Why?
In this case iPad only uses one Exchange calendar, can the problem be Exchange specific?
/* Get the Exchange Calendar */
EKEventStore* store = [[EKEventStore alloc] init];
NSError* error = nil;
NSMutableArray* calendars = [[store calendarsForEntityType:EKEntityTypeEvent] mutableCopy];
NSMutableArray* cals = [[NSMutableArray alloc] init];
for (EKCalendar *cal in calendars) {
if (cal.type == EKCalendarTypeExchange) {
[cals addObject:cal];
break;
}
}
/* get the correct event, get Events with startDate & endDate and differ with eventId */
// (tappedEvent = event to remove)
NSPredicate *predicate = [store predicateForEventsWithStartDate:[tappedEvent valueForKey:#"from"] endDate:[tappedEvent valueForKey:#"to"] calendars:cals];
NSArray *theEvents=[store eventsMatchingPredicate:predicate];
NSString *recurrenceEventIdentifier = [tappedEvent valueForKey:#"appID"];
for(EKEvent * theEvent in theEvents){
if([theEvent.eventIdentifier isEqualToString: recurrenceEventIdentifier] && ![store removeEvent:theEvent span:EKSpanThisEvent error:&error]){
NSLog(#"Error in removing event: %#",error);
}
}
[store commit:&error];
if(error){
...
}

Error Domain=EKCADErrorDomain Code=1013 "The operation couldn’t be completed. (EKCADErrorDomain error 1013.)" [duplicate]

This question already has answers here:
defaultCalendarForNewEvents failed
(8 answers)
Closed 7 years ago.
EKReminder *reminder = [EKReminder reminderWithEventStore:self.eventStore];
reminder.title = #"E-Cold 1mg";
reminder.calendar = [_eventStore defaultCalendarForNewReminders];
NSDate *date = [_myDatePicker date];
// get today date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; // here we create NSDateFormatter object for change the Format of date..
[dateFormatter setDateFormat:#"YYY-MM-dd"]; //Here we can set th
NSLog(#"%#",[dateFormatter stringFromDate:date]);
EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:date];
[reminder addAlarm:alarm];
// EKRecurrenceFrequency frequency;
NSError *error = nil;
[_eventStore saveReminder:reminder commit:YES error:&error];
if (error)
{
NSLog(#"error = %#", error);
}`
above code is set to alarm is fine but When i reset the simulator call this method showing the this error Error getting default calendar for new reminders:
Error Domain=EKCADErrorDomain Code=1013 "The operation couldn’t be completed. (EKCADErrorDomain error 1013.)"
error = Error Domain=EKErrorDomain Code=1 "No calendar has been set." UserInfo=0x7f8fca4eac80 {NSLocalizedDescription=No calendar has been set.}
and the again stop and build the application working fine.Why this error coming first time launching
EKEventStore *eventStore = [[[EKEventStore alloc] init] autorelease];
if ([eventStore respondsToSelector:#selector(requestAccessToEntityType:completion:)]) {
// iOS 6 and later
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted) {
// code here for when the user allows your app to access the calendar
[self performCalendarActivity:eventStore];
} else {
// code here for when the user does NOT allow your app to access the calendar
}
}];
} else {
// code here
[self performCalendarActivity:eventStore];
}
or may be have following problem
Quick Fix:
Goto Settings
Select Privacy
Select Reminders
Choose your application and allow access of "Reminders" to ON.

How to call instance variable programmatically

The objective is to create a reminder event via Apple's documentation. So far I have the instance variable created (and implemented in the header file as well).
- (EKReminder *)reminderWithEventStore:(EKEventStore *)eventStore {
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = webTitle;
event.notes = urlField.text;
event.startDate = [[NSDate alloc] init];
event.endDate = [[NSDate alloc] initWithTimeInterval:600 sinceDate:event.startDate];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
}
How do I create the reminder by executing the code upon clicking a button within a UIActionSheet? I've tried [self.eventStore:self]; but I'm guessing theres more to it than just that.
Here You are just adding an event to the default Calendar . The event is added , once you call this function.
Simply Create an Object of EKEventStore
EKEventStore *eventStore = [[EKEventStore alloc] init];
and call your function :-
[self reminderWithEventStore:eventStore];
But you are not returning anything in your non-void function :) , but , the function definition is already documented in iOS API version 6 for EKReminder Apple, which is a class Method.
+ (EKReminder *)reminderWithEventStore:(EKEventStore *)eventStore
are you trying to override the above method.
You can simply add a reminder in the calendar using above class method.
First you should be clear of your motive , whether you want to add
1 Event
2 Reminder
3 Calendar.

Resources