从 EkCalendar 获取过去的事件 iOS
Get past events from an EkCalendar iOS
我正在尝试从特定日历 (EkCalendar) 中获取过去的事件,但我可以获得的最早事件是 1 年前的。有没有办法获取超过一年的事件?
注意
关于这个主题有一些问题,但答案不是很清楚或似乎不起作用。
例如:Past events in EKCalendar
谢谢
我终于找到了解决办法,也找到了无法获取往事的原因。显然,iOS 不允许在超过 4 年的谓词上获取事件。
这里是答案:
Fetch all events from EventStore EventKit iOS
NSDate *start = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:currentDate options:0];
NSDate *finish = [NSDate new];
NSArray *calendarArray = [NSArray arrayWithObject:calendar];
NSMutableDictionary *dicEvents = [NSMutableDictionary dictionaryWithCapacity:1024];
NSDate *currentStart = [NSDate dateWithTimeInterval:0 sinceDate:start];
int seconds_in_year = 60 * 60 * 24 * 365;
while ([currentStart compare:finish] == NSOrderedAscending) {
NSDate *currentFinish = [NSDate dateWithTimeInterval:seconds_in_year sinceDate:currentStart];
if ([currentFinish compare:finish] == NSOrderedDescending) {
currentFinish = [NSDate dateWithTimeInterval:0 sinceDate:finish];
}
NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:currentStart endDate:currentFinish calendars:calendarArray];
[self.eventStore enumerateEventsMatchingPredicate:predicate
usingBlock: ^(EKEvent *event, BOOL *stop) {
if (event) {
[dicEvents setObject:event forKey:event.eventIdentifier];
}
}];
currentStart = [NSDate dateWithTimeInterval:(seconds_in_year + 1) sinceDate:currentStart];
}
return [dicEvents allValues];
我正在尝试从特定日历 (EkCalendar) 中获取过去的事件,但我可以获得的最早事件是 1 年前的。有没有办法获取超过一年的事件?
注意 关于这个主题有一些问题,但答案不是很清楚或似乎不起作用。 例如:Past events in EKCalendar
谢谢
我终于找到了解决办法,也找到了无法获取往事的原因。显然,iOS 不允许在超过 4 年的谓词上获取事件。
这里是答案: Fetch all events from EventStore EventKit iOS
NSDate *start = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:currentDate options:0];
NSDate *finish = [NSDate new];
NSArray *calendarArray = [NSArray arrayWithObject:calendar];
NSMutableDictionary *dicEvents = [NSMutableDictionary dictionaryWithCapacity:1024];
NSDate *currentStart = [NSDate dateWithTimeInterval:0 sinceDate:start];
int seconds_in_year = 60 * 60 * 24 * 365;
while ([currentStart compare:finish] == NSOrderedAscending) {
NSDate *currentFinish = [NSDate dateWithTimeInterval:seconds_in_year sinceDate:currentStart];
if ([currentFinish compare:finish] == NSOrderedDescending) {
currentFinish = [NSDate dateWithTimeInterval:0 sinceDate:finish];
}
NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:currentStart endDate:currentFinish calendars:calendarArray];
[self.eventStore enumerateEventsMatchingPredicate:predicate
usingBlock: ^(EKEvent *event, BOOL *stop) {
if (event) {
[dicEvents setObject:event forKey:event.eventIdentifier];
}
}];
currentStart = [NSDate dateWithTimeInterval:(seconds_in_year + 1) sinceDate:currentStart];
}
return [dicEvents allValues];