EKEventStore removeEvent EKErrorDomain Code=11 EKErrorObjectBelongsToDifferentStore - iphone

I am getting an error:
removing event error: Error Domain=EKErrorDomain Code=11 "That event does not belong to that event store." UserInfo=0x1fdf96b0 {NSLocalizedDescription=That event does not belong to that event store.
When I try to remove an EKEvent I just created.
The code below shows that I am storing the eventIdentifier and using it to retrieve the event. Furthermore when I do this, and NSLog the event I can see all the properties of it correctly.
From all the examples I have seen I am doing everything correctly. I also NSLog'ed the EKEventStore's eventStoreIdentifier and its the same every time I access it in any of my methods so it should be the same EKEventStore.
Any help would be greatly appreciated.
- (EKEvent *) getCurrentCalendarEvent {
NSString *currentCalendarEventID = [[UserModel sharedManager] currentCalendarEventID];
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [eventStore eventWithIdentifier:currentCalendarEventID];
return event;
}
- (void) removeCurrentCalendarEvent {
EKEvent *event = [self getCurrentCalendarEvent];
if (event) {
NSError *error;
EKEventStore *eventStore = [[EKEventStore alloc] init];
[eventStore removeEvent:event span:EKSpanFutureEvents error:&error];
}
}
- (void) addCurrentCalendarEvent {
[self removeCurrentCalendarEvent];
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = [[UserModel sharedManager] reminderLabel];
event.notes = #"Notes";
NSDate *startDate = [NSDate date];
int futureDateSecs = 60 * 5;
NSDate *endDate = [startDate dateByAddingTimeInterval:futureDateSecs];
event.startDate = startDate;
event.endDate = endDate;
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *error;
[eventStore saveEvent:event span:EKSpanThisEvent error:&error];
[[UserModel sharedManager] setCurrentCalendarEventID:event.eventIdentifier];
[[UserModel sharedManager] saveToDefaults];
}

This is happening because you are always initializing a new instance of EKEventStore. When you are adding EKEvent to EKEventStore then the instance of EKEventStore is different then when you are trying to remove. What you can do is that declare EKEventStore reference variable in .h and initialize it only one time.
in .h -
EKEventStore *eventStore;
in .m -
inside viewDidLoad -
eventStore = [[EKEventStore alloc] init];
then remove this line from all of three methods-
EKEventStore *eventStore = [[EKEventStore alloc] init];

Related

adding event does not show up in iPhone calendar

Trying programistically add an event to iPhone calendar.
I get no errors, however, nothing gets added to the calendar either.
savedEventId(below) returns (null)
here is the code:
Methods
Adding event :
if(accessGranted){
EKEvent *event = [EKEvent eventWithEventStore:store];
event.startDate =[NSDate date];
event.endDate = [event.startDate dateByAddingTimeInterval:60*60];
[event setCalendar:[store defaultCalendarForNewEvents]];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
NSString *savedEventId = event.eventIdentifier;
}
getting access to calendar
in viewDidLoad:
store = [[EKEventStore alloc] init];
accessGranted=FALSE;
[self.store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
accessGranted=TRUE;
}];
in .h file
#import <EventKit/EventKit.h>
#interface
EKEventStore *store;
BOOL accessGranted;
#property(nonatomic, retain) EKEventStore *store;
in .m file
#synthesize store;
Try this code instead:
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) { return; }
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = #"Event Title";
event.startDate = [NSDate date]; //today
event.endDate = [event.startDate dateByAddingTimeInterval:60*60]; //set 1 hour meeting
[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
}];
Remember to:
request access to the user's calendar via "requestAccessToEntityType:completion:"
commit your event

How to delete event from iCal added from my app.?

I'm developing an app in which when user add any event as his favorite, I'm adding that event to iCal but how do i remove that event from iCal if user removes that particular event from his favorite.?
Here is my code:
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[[self.parentDetailArray valueForKey:#"start_time_num"] intValue]];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc]init];
[dateformatter setLocale:[NSLocale currentLocale]];
[dateformatter setDateFormat:#"dd-MM-yyyy"];
NSString *dateString=[dateformatter stringFromDate:date];
EKEventStore *eventStore = [[EKEventStore alloc] init];
if([eventStore respondsToSelector:#selector(requestAccessToEntityType:completion:)])
{
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted){
//---- codes here when user allow your app to access theirs' calendar.
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = title;
event.startDate = date;
event.endDate = [[NSDate alloc] initWithTimeInterval:1000 sinceDate:event.startDate];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
// Here I'm trying to get the identifier of that event but m getting only (null)
self.str = [[NSString alloc] initWithFormat:#"%#", event.eventIdentifier];
[self.arrayofEventId addObject:self.str];
//[self performCalendarActivity:eventStore];
}else
{
//----- codes here when user NOT allow your app to access the calendar.
}
}];
}
else {
//---- codes here for IOS < 6.0.
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = #"Testing for calendar";
event.startDate = [[NSDate alloc] init];
NSLog(#"%#",event.startDate);
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
//[self performCalendarActivity:eventStore];
}
How do i get the event identifier n remove that particular event from iCal. any help would be appreciated.
You could write all app generated event ID's to file and then load them back up next time the app loads. This way you can keep track of which events your app created. Then you may want to try this method:
- (BOOL)removeEvent:(EKEvent *)event span:(EKSpan)span commit:(BOOL)commit error:(NSError **)error
Save with this:
NSString *id = [[NSString alloc] initWithFormat:#"%#", event.eventIdentifier];
Remove with this:
EKEvent *event = [eventStore eventWithIdentifier:id];
NSError *error = nil;
[eventStore removeEvent:event span:EKSpanThisEvent error:&error];
Apple has documentation on the EKEventStore here.

EKEvent alarm AlertView disappears in less then a second

Event is successfully added with alarm that plays ten seconds (for testing purposes) before event start time. Problem is that alarm triggers (UIAlertView on iPhone shows) but it dissapears in less then a second. Very strange...
What could be the problem?
Code:
- (IBAction)addEvent:(id)sender {
EKEventStore *eventStore = [[EKEventStore alloc] init];
if ([eventStore respondsToSelector:#selector(requestAccessToEntityType:completion:)])
{
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
EKEvent *anEvent = [EKEvent eventWithEventStore:eventStore];
anEvent.calendar = eventStore.defaultCalendarForNewEvents;
anEvent.title = self.eventNameTextField.text;
NSDate *date = self.datePicker.date;
anEvent.startDate = date;
anEvent.endDate = date;
NSMutableArray *myAlarmsArray = [[NSMutableArray alloc] init];
EKAlarm *alarm1 = [EKAlarm alarmWithRelativeOffset:-10];
[myAlarmsArray addObject:alarm1];
anEvent.alarms = myAlarmsArray;
NSError *eventError;
[eventStore saveEvent:anEvent span:EKSpanThisEvent error:&eventError];
}];
}
}
see the following code hope it will help you.
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = [NSString stringWithFormat:#"%#'s Birthday",strName];
NSDate *twoYearsFromNow = [NSDate dateWithTimeIntervalSinceNow:1577846275];
double alarmAmountInSeconds = 60*60*9;
EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:(1.0*alarmAmountInSeconds)];
EKRecurrenceRule *recurrance;
recurrance = [[EKRecurrenceRule alloc] initRecurrenceWithFrequency:EKRecurrenceFrequencyYearly interval:1 end:[EKRecurrenceEnd recurrenceEndWithEndDate:twoYearsFromNow]];
NSMutableArray *arrayReccurence = [NSMutableArray arrayWithObject:recurrance];
event.recurrenceRules = arrayReccurence;
event.startDate = startDate;
event.endDate = [[NSDate alloc] initWithTimeInterval:600 sinceDate:event.startDate];
event.allDay = true;
event.alarms = [NSArray arrayWithObject:alarm];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];

Cleaning up memory after using EKEvent

Was looking at some code and it looks to be leaking memory. And I'm not sure should I clean this up? Or is it ok?
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
I would have guessed this is an autorelease since its a connivence method.
But when i read
event.startDate = [[NSDate alloc] init];
I see an alloc and an init, so I get nervous about wondering will it be leaking.
Full block of code below:
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = #"Test Event for Code Demo";
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];
[eventStore release];
Thanks,
-Code
I don't think the properties startDate and endDate need to be alloc'd and init'd. You are creating an autoreleased object with your current code.
EKEvent *event = [EKEvent eventWithEventStore:eventStore]; // autoreleased this way
To fill your dates and properties, try an alternate method to fill.
event.startDate = [NSDate date];
event.endDate = [NSDate dateWithTimeInterval:600 sinceDate:event.startDate];
Now you just need to release eventStore like you are currently doing. Hope this helps.
If you are using EKEventViewController, Apple documentation says following:
#property(nonatomic, retain) EKEvent *event
Discussion
This property must be set before the view is displayed.
EKEventViewController Documentation

Find iOS Calendars

I have an app that creates a calendar event programmatically it is working and I just made it so it retrieves the users calendars and displays them in a picker but I have a memory leak. Can you see it becasue I tried releasing everything... In addition my main issue is how do I have it save to that calendar that the user selects, [event setCalendar:calendararray]; doesn't work. The calendararray is a EKCalendar *calendararray and I am setting the user selected calendar to it. Why does this not work???? How do I make it work...
calendar .m
#import "calendar.h"
#implementation calendar
#synthesize delegate;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
EKEventStore *eventStore = [[EKEventStore alloc] init];
/* These are the calendar types an iOS Device can have. Please note
that the "type" property of an object of type EKCalendar
is of type EKCalendarType. The values in the "CalendarTypes"
array reflect the exact same values in the EKCalendarType
enumeration, but as NSString values */
NSArray *calendarTypes = [NSArray arrayWithObjects:
#"Local",
#"CalDAV",
#"Exchange",
#"Subscription",
#"Birthday",
nil];
/* Go through the calendars one by one */
NSUInteger counter = 1;
for (EKCalendar *thisCalendar in eventStore.calendars){
/* The title of the calendar */
NSLog(#"Calendar %lu Title = %#",
(unsigned long)counter, thisCalendar.title);
EKEventStore has a calendars property which is an NSArray of EKCalendar instances. You can then get each EKCalendar's title and display all of them to the user so they can select the calendar they wish to add the new event to.
NSLog(#"Calendar %lu Title = %#", (unsigned long)counter, thisCalendar.title);
NSString *title = [NSString stringWithFormat:#"Calendar %lu Title = %#", (unsigned long)counter, thisCalendar.title];
NSLog(title);
I'm not sure if all the code you posted is from the same version, but if you threw that together I'd see at least the following problem:
You say
[event setCalendar:calendararray];
doesn't work.
It seems calendararray ist set in eventview.m's calendararray: method.
This in turn is called from calendar.m's pickerView:didSelectRow:, where it takes the selected object from arrayColors.
In calendar.m's viewDidLoad method, arrayColors is initialised with the users's calendars' titles, not the calendars.
So you end up giving EKEvent's setCalendar: method an NSString instead of an EKCalendar.
Not knowing the rest of your program, as a fix I would try to keep the EKCalendar objects themselves in arrayColors.
So, in calendar.m try to change the
[arrayColors addObject:thisCalendar.title];
in viewDidLoad to
[arrayColors addObject:thisCalendar];
Then change pickerView:titleForRow: to
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
EKEvent *event = [arrayColors objectAtIndex:row]
return event.title;
}
FInally, in eventview.m change calendararray:
- (void) calendararray:(EKCalendar *)array{
NSLog(#"calendarNameTextFieldStringFromTable %#", array);
calendararray = array;
calendarLabel.text = array.title;
calendarLabel.textColor = [UIColor brownColor];
}
Try this way:
#import "EventTestViewController.h"
#import "EventKit/EventKit.h"
#implementation EventTestViewController
- (void)viewDidLoad {
[super viewDidLoad];
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = #"EVENT TITLE";
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];
}
Ok i see what you want, heres my way:
EKEventStore *eventStore = [[EKEventStore alloc] init];
NSArray *calendarArray = [eventStore calendars]; //Here you get all the device calendars
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = #"Event Title";
event.startDate = [[NSDate alloc] init]; event.endDate = [[NSDate alloc] initWithTimeInterval:816 sinceDate:event.startDate];
[event setCalendar:[calendarArray objectAtIndex:1]]; //here you set which calendar you want..
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
You can NSLog with a FOR loop iterating "index" using this code below, to know which one you want.
NSLog(#"Cal name> %#. Index of cal> %i", [[calendarArray objectAtIndex:index] title], index);
Hope this helps