开发者

Deleting an event from iPhone's calendar

开发者 https://www.devze.com 2023-01-15 00:48 出处:网络
I am trying to delete an event from the Calendar on user request. This is what I\'ve come up with: // Deleting Event

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.currentDate开发者_StackOverflow中文版Name;
[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

0

精彩评论

暂无评论...
验证码 换一张
取 消