从iPhone的日历中删除事件

时间:2010-09-08 06:06:16

标签: ios objective-c cocoa-touch

我正在尝试从用户请求的日历中删除事件。这就是我想出来的:

// 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];

下面是我正在调用从事件数组中删除事件的函数。 Items数组用于从iPhone日历中获取事件

- (BOOL)removeEvent:(EKEvent *)event span:(EKSpan)span error:(NSError **)error{
    VoiceRecorderAppDelegate *appDelegate = (VoiceRecorderAppDelegate *)[[UIApplication sharedApplication] delegate];
    [items removeObjectAtIndex:appDelegate.objectindexpath];
}

2 个答案:

答案 0 :(得分:16)

首先,在向日历添加/保存事件时保存事件的eventId。

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

然后标识要删除的事件,然后删除该事件。

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

另外,不要忘记从arrayofEventId中删除该事件。

答案 1 :(得分:1)

您可以通过以下方式实现此目的:

  

使用您的日期范围创建NSpredicate   想要删除事件,86400是事件中一天的持续时间   这段代码我删除了一个月的旧事件。我正在使用   调度队列,如没有。获取的事件数量可能很大,并且可以保持UI免费。

首先创建事件存储并检查访问权限(访问检查仅需要iOS6以上版本):

    - (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];
        }
    }

然后从商店中删除事件:

    - (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
            });
        });
    }

这是漫长的方式,用它来批量删除事件。但是如果你只需要删除一个事件,那么将事件标识符保存到`NSUserDefaults(生成事件后)

[eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:NULL];
[[NSUserDefaults standardUserDefaults] setObject:[event eventIdentifier] forKey:@"Event ID"];

然后在使用

删除时将其取回
[eventStore eventWithIdentifier:@"Event ID"];

然后使用

将其从商店中删除
[eventStore removeEvent:event span:EKSpanThisEvent commit:YES error:NULL];

有关获取事件或日历的其他方法的更多说明,请参阅EventStore docs: http://developer.apple.com/library/ios/#documentation/EventKit/Reference/EKEventStoreClassRef/Reference/Reference.html#//apple_ref/doc/uid/TP40009567Calendar and Reminder Programming guide: http://developer.apple.com/library/ios/#documentation/DataManagement/Conceptual/EventKitProgGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40009765