从iPhone的日历中删除事件
我试图从用户请求的日历中删除一个事件。 这就是我想出来的:
// 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]; }
首先,在向日历添加/保存事件的同时保存事件的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中删除该事件。
您可以通过以下方式实现此目的:
通过使用您要删除事件的日期范围创建
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 EventStore docs:
//developer.apple.com/library/ios/#documentation/EventKit/Reference/EKEventStoreClassRef/Reference/Reference.html#// apple_ref / doc / uid / TP40009567或Calendar and Reminder Programming guide:
http Calendar and Reminder Programming guide:
//developer.apple.com/library/ios/#documentation/DataManagement/Conceptual/EventKitProgGuide/Introduction/Introduction.html#//apple_ref/doc / UID / TP40009765