如何从iOS中的日历中删除旧事件

维迪亚南德

在我的应用中,我将事件保存到日历中。

但是,如果该活动是从2015年2月1日到2015年2月20日。

我如何删除2015年2月1日至2015年2月15日之间的活动,因为这些活动已完成。

我用Google搜索并使用iPhone的“设置”选项找到了答案

https://apple.stackexchange.com/questions/103570/auto-delete-previous-old-events-from-ios-7-calendar

我使用以下代码删除日历中的所有事件

NSDate *startDate = [NSDate date];
NSDate* endDate =  [NSDate dateWithTimeIntervalSinceNow:[[NSDate distantFuture] timeIntervalSinceReferenceDate]];
NSArray *calendarArray = [NSArray arrayWithObject:self.defaultCalendar];
NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate                                                                endDate:endDate                                                                        calendars:calendarArray];

NSArray *events = [self.eventStore eventsMatchingPredicate:predicate];
for (EKEvent *event in events)
{
  NSError* err = nil;
 [self.eventStore removeEvent:event span:EKSpanFutureEvents commit:YES error:&err];
}

但是可以通过任何方式删除已完成的旧活动。其他方式可以通过编程方式删除早于2015年2月的活动。

注意:如果未设置“结束日期”,我正在使用需要14天的重复事件。

任何想法和建议如何修复..

提前致谢..!

维迪亚南德

我找到了解决方案。以下代码将删除过去2天到1年之前的所有事件。

源链接获取所有事件

    NSDate *today = [NSDate date];

    NSDateComponents *components = [NSDateComponents new];

    [components setDay:-2];

    NSDate *endDate = [[NSCalendar currentCalendar] dateByAddingComponents:components
                                                                        toDate:today
                                                                       options:kNilOptions];

    NSDate* startDate =  [NSDate dateWithTimeIntervalSinceNow:[[NSDate distantPast] timeIntervalSinceReferenceDate]];

        // use Dictionary for remove duplicates produced by events covered more one year segment
    NSMutableDictionary *eventsDict = [NSMutableDictionary dictionaryWithCapacity:1024];

    NSDate* currentStart = [NSDate dateWithTimeInterval:0 sinceDate:startDate];

        int seconds_in_year = 60*60*24*365;

        // enumerate events by one year segment because iOS do not support predicate longer than 4 year !
        while ([currentStart compare:endDate] == NSOrderedAscending) {

    NSDate* currentFinish = [NSDate dateWithTimeInterval:seconds_in_year sinceDate:currentStart];

    NSArray *calendarArray = [NSArray arrayWithObject:self.defaultCalendar];

    if ([currentFinish compare:endDate] == NSOrderedDescending) {
                currentFinish = [NSDate dateWithTimeInterval:0 sinceDate:endDate];
            }
    NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:currentStart endDate:currentFinish calendars:calendarArray];
    [self.eventStore enumerateEventsMatchingPredicate:predicate
                                              usingBlock:^(EKEvent *event, BOOL *stop) {

                                                  if (event) {
                                                      [eventsDict setObject:event forKey:event.eventIdentifier];
                                                  }

                                              }];
   currentStart = [NSDate dateWithTimeInterval:(seconds_in_year + 1) sinceDate:currentStart];

   }

  NSArray *events = [eventsDict allValues];

        for (EKEvent *event in events) {
            NSError* err = nil;
            [self.eventStore removeEvent:event span:EKSpanFutureEvents commit:YES error:&err];

        }

今天的日期是2015年2月17日。以上内容将删除2015年2月15日当天或之前的所有活动。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何从Thingsboard删除旧事件?

如何删除日历中的特定事件

iOS Swift FSCalendar 从日历中删除事件点

从iOS中的设备删除应用程序后,如何删除“我的应用程序日历”中的所有事件

如何从Android日历事件中删除与会者?

如何从默认日历中删除所有事件

如何从Gnome日历中删除日历?

从日历中删除所有事件

在iOS中获取定期日历事件

删除全部日历中的所有事件后,如何添加事件

拉拉维尔 | 删除功能 - 如何从日历事件中删除照片

从Angular UI日历事件中删除事件时间

如何禁用 ZK 日历中的事件幽灵?

如何在Android中添加日历事件?

如何在日历中创建重复事件?

如何在Yii中创建日历事件?

如何在定制的日历中呈现事件

在iOS中通过QR扫描仪扫描时如何在日历中添加事件

如何在日历中自动删除事件后获得结束时间?

我如何知道某个事件是否已在Google日历中删除?

如何使用API v3 .NET删除Google日历中的事件?

在Mac版Outlook 2016中,如何删除主日历的所有事件?

如何从日历中删除所有面板?

全日历。从外部列表中删除拖动的事件

从Google日历中删除的事件仍显示在listEvents上

从面板的日历指示器中删除事件

如何使用 Swift 在 iOS 日历应用程序中编辑事件日期?

在iOS EventKit中自定义日历事件/警报?

如何使用Graph API和C#访问除日历日历以外的日历中的事件