Can't delete single ekevent of recurrent event in calendar iOS - 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){
...
}

Related

Deleting and recreating EKCalendar

In my app, I create events in an EKCalendar. I fetch the events online, and in order to refresh the events, I want to first delete the calendar (if it exists), recreate it, and then put the new events in there.
To instantiate the calendar I use
- (EKCalendar *)calendar {
if (!_calendar) {
NSArray *calendars = [self.store calendarsForEntityType:EKEntityTypeEvent];
NSString *calendarTitle = #"MyCalendar";
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"title matches %#", calendarTitle];
NSArray *filtered = [calendars filteredArrayUsingPredicate:predicate];
if ([filtered count]) {
_calendar = [filtered firstObject];
} else {
_calendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:self.store];
_calendar.title = calendarTitle;
_calendar.source = self.store.defaultCalendarForNewEvents.source;
NSError *calendarErr = nil;
BOOL calendarSuccess = [self.store saveCalendar:_calendar commit:YES error:&calendarErr];
if (!calendarSuccess) {
NSLog(#"Calendar Error = %#", [calendarErr localizedDescription]);
}
}
}
return _calendar;
}
To delete the calendar, I use
-(IBAction)deleteCalendar{
NSError *error = nil;
[self.store removeCalendar:_calendar commit:YES error:&error];
}
Both methods work fine individually.
So, when I start the creation of events, I do the following:
[self deleteCalendar];//delete calendar and its events, in case it already exists
[self calendar];//create calendar
[self importEvents];//put events in calendar
Now, what I observe is the following:
On the first run of the app
a calendar is created
events are imported. (This is expected, and works just fine)
While the app is running, I trigger the above methods again with a button. With the following, for me puzzling, result:
the calendar is deleted (expected result)
NO calendar is created (WHY? that is my main question).The "if (!_calendar)" part of the method is considered FALSE, and nothing is executed.
The 'importEvents' method runs through its regular hoopla, without any apparent errors, although I would expect something like a 'no source' error.
Please advise.
UPDATE:
This could be an indicator of what is happening, but I still don't get it:
After a while, the events appear in a different calendar, i.e. not the calendar called 'myCalendar', but another, iCloud based calendar, apparently the one that at that point is the defaultCalendarForNewEvents. However, that also doesn't make any sense to me.
OK, so, what is happening:
I have deleted the Calendar from the store, effectively, but a reference to that calendar actually was still hanging around in my app.
I solved it as follows:
-(IBAction)deleteCalendar:(id)sender{
NSError *error = nil;
if(_calendar){
[self.store removeCalendar:_calendar commit:YES error:&error];
}
_calendar = nil;
}
I hope this is useful to someone

Have the endHourTime of my event in my calendar iOS : Obj-C

I would like to have the actual event, which is synchronised on my calendar app.
The actual event is the event which is happening now.
I would like, at first, to make an app which tells me how much time remains before the end of my event.
The event is my calendar synchronized to my iphone.
In my Main.StoryBoard, I declared a Label:
#property (strong, nonatomic) IBOutlet UILabel *currentEvent;
I declared a EKEventStore:
EKEventStore *store = [[EKEventStore alloc] init];
From now, I know how to create an event, with start date, end date.. but I would like to retrieve the information from my current event, to be able to do: endDate - timeActual;
Next, in my Label, it will be written : 57 minutes before the end of the event.
NSLog(#"%i minutes before the end of the event.", remainTime );
I don't know if I'm clear, tell me if you don't understand what I'm asking?
Thank you for your help! :)
[Update]
I know how to have the identier of my calendar :
EKEventStore *store = [[EKEventStore alloc] init];
store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted)
NSLog(#"Access to store not granted");
}];
NSArray *calendars = [store calendarsForEntityType:EKEntityTypeEvent];
for (EKCalendar *calendar in calendars)
{
NSLog(#"Calendar = %#", calendar.description);
self.currentEvent.text = [NSString stringWithFormat:#"%#", calendar.calendarIdentifier];
}
EKCalendar *myCalendar = calendars[0];
NSLog(#"My Calendar = %#", myCalendar);
EKEvent *actualEvent = [EKEvent eventWithEventStore:store];
I know how to set event on my calendar, but no have events which are in my calendar!
Have you got any ideas ?

iOS - Delete Recurring EKEvent, event appears again

I have a recurring event in calendar. I'm delete a single event using this code [store removeEvent:event span:EKSpanThisEvent commit:YES error:&errorThis]; and this methods returns true but the event is not deleted from the calendar.
On EKCalendarItem Class reference using the property calendarItemExternalIdentifier you find this
Recurring event identifiers are the same for all occurrences. If you wish to differentiate between occurrences, you may want to use the start date
So you want delete only a recurrence you have to do something like this:
NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:calendars];
NSArray *theEvents = [eventStore eventsMatchingPredicate:predicate];
NSString *recurrenceEventIdentifier;
for(EKEvent * theEvent in theEvents)
{
if([theEvent.eventIdentifier isEqualToString: recurrenceEventIdentifier]
&& ![eventStore removeEvent:theEvent span:EKSpanThisEvent error:&error])
{
NSLog(#"Error in removing event: %#",error);
}
}
Your method instead, deletes only the first occurrence. If you want delete all recurring events just change "span" parameter in EKSpanFutureEvents.
EDIT: Now only deletes the matching recurrent event, not everything.
Please make sure you have only one instance of EKEventStore in singleton pattern in your app :
static EKEventStore *eventStore = nil;
+ (EKEventStore *)getEventStoreInstance
{
if (eventStore == nil){
#synchronized(self){
if (eventStore == nil){
eventStore = [[EKEventStore alloc] init];
}
}
}
return(eventStore);
}

Create a Calendar (in the ios native calendars) through an Application on the iphone

I want to know if it is possible to create a new calendar for your iphone through an application. I know you can set up event with an app, but can I make another calendar through the app to place all the events too.
EDIT:
IF it is not possible what would be the best work around, make a calendar and event store, and save that information with nscoding? Then retrieve afterwards?
If there is a possibilty to create a new calendar to integrate with the existing calendars, that is what I want to do.
This is how I have done this:
-(NSString*)createCal:(NSString*)myCalId;{
// Instantiate eventstore object
EKEventStore *store = [[EKEventStore alloc] init];
EKSource *localSource = nil;
for (EKSource *source in store.sources)
if (source.sourceType == EKSourceTypeLocal){
localSource = source;
break;
}
//this is you creating your calendar
EKCalendar *cal;
cal = [EKCalendar calendarWithEventStore:store];
cal.title = #"Name of calendar";
cal.source = localSource;
[store saveCalendar:cal commit:YES error:nil];
NSLog(#"cal id = %#", cal.calendarIdentifier);
return cal.calendarIdentifier;}
You will need to import <EventKit/EventKit.h>
This is powerful so you need to be careful and do a lot of verifications for example if the calendar already exist, and so on. Hope this helps.
Looking at the documentation for EventKit, it doesn't seem possible to create new calendars programatically. However, you can get a list of existing calendars and find out the default calendar.
Hope this helps.
You can't use the standard init-Method anymore; it's deprecated. Here the quote from the EKEventStore Class Reference:
"On iOS 5 and later, initialize an event store object with the default init method. On iOS 6 and later, you must request access to an entity type after the event store is initialized with requestAccessToEntityType:completion: for data to return.
On OS X, use initWithAccessToEntityTypes: instead of the default init method. Acceptable entity types are EKEntityMaskEvent for events and EKEntityMaskReminder for reminders."
So if you use iOS6, it's somethin like:
EKEventStore *store = [[EKEventStore alloc] initWithAccessToEntityTypes:completion:];
and
cal = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:store];
I need it for my MacOSX App. So i have to use this:
So in my Case (MacOSX) it's this solution. It's Sparqs code; just modify with the new init instead of the the standard one:
-(NSString*)createCal:(NSString*)myCalId;{
// Instantiate eventstore object
EKEventStore *store = [[EKEventStore alloc] initWithAccessToEntityTypes:EKEntityMaskEvent];
EKSource *localSource = nil;
for (EKSource *source in store.sources)
if (source.sourceType == EKSourceTypeLocal){
localSource = source;
break;
}
//this is you creating your calendar
EKCalendar *cal;
cal = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:store];
cal.title = #"Name of calendar";
cal.source = localSource;
[store saveCalendar:cal commit:YES error:nil];
NSLog(#"cal id = %#", cal.calendarIdentifier);
return cal.calendarIdentifier;}

Deleting an event from iPhone's calendar

I am trying to delete an event from the Calendar on user request. This is what I've come up with:
// Deleting Event
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = appDelegate.title1;
event.startDate = appDelegate.recAddDate;
event.endDate = appDelegate.currentDateName;
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore removeEvent:event span:EKSpanThisEvent error:&err];
Below is the function I'm calling to remove the event from the event array. Items array is used to fetch events from iPhone calendar
- (BOOL)removeEvent:(EKEvent *)event span:(EKSpan)span error:(NSError **)error{
VoiceRecorderAppDelegate *appDelegate = (VoiceRecorderAppDelegate *)[[UIApplication sharedApplication] delegate];
[items removeObjectAtIndex:appDelegate.objectindexpath];
}
Firstly, save the eventId for the event while adding/saving events to the calendar.
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
NSString* str = [[NSString alloc] initWithFormat:#"%#", event.eventIdentifier];
[arrayofEventId addObject:str];
and then identify the event you want to remove ande then remove that event.
EKEventStore* store = [[EKEventStore alloc] init];
EKEvent* eventToRemove = [store eventWithIdentifier:[arrayofEventId objectAtIndex:i]];
if (eventToRemove != nil) {
NSError* error = nil;
[store removeEvent:eventToRemove span:EKSpanThisEvent error:&error];
}
Also don't forget to remove that event from arrayofEventId as well.
You can achieve this in the following ways:
By creating an NSpredicate using the date range withing which you
want to delete events, 86400 being the duration of a day in events, in
this piece of code I am deleting month old events. I am using a
dispatch queue, as the no. of events fetched may be large, and to keep the UI free.
First Create the event store and check access(access check required only iOS6 onwards):
- (void)addEventsToCalendar {
EKEventStore *eventStore = [[EKEventStore alloc] init];
if ([eventStore respondsToSelector:#selector(requestAccessToEntityType:completion:)]) {
//implementation for devices running OS version iOS 6.0 onwards.
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted) {
[self removeEventsFromStore:eventStore];
} else {
//perform for No Access using Error
}];
} else {
//implementation for devices running OS version lower than iOS 6.0.
[self removeEventsFromStore:eventStore];
}
}
Then remove events from the store:
- (void)removeEventsFromStore:(EKEventStore*)eventStore {
NSDate *startDate = [NSDate dateWithTimeIntervalSinceNow:-30 * 86400];
NSDate *endDate = [NSDate date];
dispatch_queue_t queue = dispatch_queue_create("com.arc.calendar", NULL);
dispatch_async(queue, ^{
NSArray *calendarArray = [NSArray arrayWithObject:[PWCCalendar getCalendarForEventStore:eventStore]];
NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:startDate endDate:[NSDate dateWithTimeInterval:ONE_DAY_DURATION sinceDate:endDate] calendars:calendarArray];
NSArray *eventArray = [eventStore eventsMatchingPredicate:predicate];
for (EKEvent *event in eventArray) {
[eventStore removeEvent:event span:EKSpanThisEvent commit:YES error:NULL];
}
dispatch_async(dispatch_get_main_queue(), ^{
//Get the main Queue and perform UPdates
});
});
}
This is the Long way, use it to delete events in bulk. But if you need to delete only One event, then save the events identifier to `NSUserDefaults(after generating the event)
[eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:NULL];
[[NSUserDefaults standardUserDefaults] setObject:[event eventIdentifier] forKey:#"Event ID"];
and then fetch it back while removing using
[eventStore eventWithIdentifier:#"Event ID"];
and then remove it from the store using
[eventStore removeEvent:event span:EKSpanThisEvent commit:YES error:NULL];
For more clarifications on the other methods to fetch events or calendar, pelase refer to EventStore docs: http://developer.apple.com/library/ios/#documentation/EventKit/Reference/EKEventStoreClassRef/Reference/Reference.html#//apple_ref/doc/uid/TP40009567 or to the Calendar and Reminder Programming guide: http://developer.apple.com/library/ios/#documentation/DataManagement/Conceptual/EventKitProgGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40009765

Resources