在我的应用中,我将事件保存到日历中。
但是,如果该活动是从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] 删除。
我来说两句