2

I'm trying to remove events that i have created from the iPhone calendar.

I tried this, but it always returns NO:

  [eventStore removeEvent:event span:EKSpanThisEvent error:&err];

I created the event as follows and it works:

eventStore = [[EKEventStore alloc] init];

event = [EKEvent eventWithEventStore:eventStore];

event.title = @"EVENT TITLE";
NSDateFormatter *   dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd:HH:mm"];


NSDate * date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:@"2010-8-15:12:30"];
[date retain];

event.startDate = date;
event.endDate = [[NSDate alloc] initWithTimeInterval:600 sinceDate:event.startDate];

[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err]; 

Is there a way to remove this event? Or it would be be better that if I try to write this event again it only modifies it instead of creating a new one.

Thanks,

Basel
  • 2,368
  • 1
  • 16
  • 21
  • try to catch the error (log the error)other wise the code looks ok . or try first simple dates like [NSDate date] for start date and end date . – harshalb Aug 14 '10 at 10:42
  • If you are removing in some other method first get the event with [eventStore eventWithIdentifier:eventID]; – harshalb Aug 14 '10 at 10:44
  • How to make sure I'm using the same eventstore? The documentation says "This method returns NO if event is not in the event store to begin with." – Basel Aug 14 '10 at 12:04
  • I am not saying about eventstore I am trying to say to save the eventIdentifier of the event like NSString *eventID = event.eventIdentifier – harshalb Aug 14 '10 at 12:10
  • thanks a lot. I'm able to delete the events now :) – Basel Aug 14 '10 at 12:22

2 Answers2

6

After creating the event I save the eventIdentifier in an array:

[eventStore saveEvent:event span:EKSpanThisEvent error:&err]; 
   NSString* str = [[NSString alloc] initWithFormat:@"%@", event.eventIdentifier];
[arrayofCalIDs addObject:str];

To delete the events:

EKEventStore* store = [[[EKEventStore alloc] init] autorelease];
   EKEvent* event2 = [store eventWithIdentifier:[arrayofCalIDs objectAtIndex:i]];
if (event2 != nil) {  
  NSError* error = nil;
  [store removeEvent:event2 span:EKSpanThisEvent error:&error];
} 
[myPath release];
Basel
  • 2,368
  • 1
  • 16
  • 21
0

Just an FYI for the answer above. It is found on the web on with this link: http://tech.vniup.com/index.php/iphone/objective-c/how-to-delete-event-from-iphone-calendar-programmatically.html

My only suggestion is that if you are building an array of objects each object ideally would be the event. Then do a reverse array operation because the latest event will always be at the bottom.

NSMutableArray *reverseArray = [NSMutableArray arrayWithCapacity:[eventsList count]];

for (id element in [eventsList reverseObjectEnumerator]) {
    [reverseArray addObject:element];
}
eventsList = reverseArray;

And also in the display of the events be nice to your users and display the start date of the event.

Anyway, after you have an array objects that are EKEvents you can do this which is MUCH easier.

EKEvent *eventToRemove = [myEventStore eventWithIdentifier:thisEvent.eventIdentifier ];
        if ([eventToRemove.eventIdentifier length] > 0) {
            NSError* error = nil;
            [myEventStore removeEvent:eventToRemove span:EKSpanThisEvent error:&error];
            }

Then you can remove that same event from you array of events for the table display....easy!

Rakib Ansary
  • 4,078
  • 2
  • 18
  • 29
tony.stack
  • 780
  • 8
  • 21