Alex Rivera | Logout

Deleting an event from iPhone's calendar

Asked 2010-09-08T06:06:16.053
14

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];
}
Edit
Report

1 Answer

1

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];
        
answered 2013-02-11T06:43:40.563

Your Answer