从 EventStore EventKit iOS 中获取所有事件 [英] Fetch all events from EventStore EventKit iOS
问题描述
我想知道如何在 iOS 中使用 EventKit 从 EventStore 中获取所有事件.
这样我可以指定今天的所有活动:
- (NSArray *)fetchEventsForToday {NSDate *startDate = [NSDate 日期];//endDate 是 1 天 = 60*60*24 秒 = 距 startDate 86400 秒NSDate *endDate = [NSDate dateWithTimeIntervalSinceNow:86400];//创建谓词.将默认日历传递给它.NSArray *calendarArray = [NSArray arrayWithObject:defaultCalendar];NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:calendarArray];//获取与谓词匹配的所有事件.NSArray *events = [self.eventStore eventsMatchingPredicate:predicate];返回事件;}
正确的应该使用 NSPredicate,它是通过以下方式创建的:
NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:calendarArray];
我尝试过使用
distantPast遥远的未来
作为 startDate 和 endDate,不好.所以其他 Q 中的其他 A 并不是我要寻找的.p>
谢谢!
<小时>编辑
我已经测试并得出结论,我最多只能获取 4 年的事件.有什么办法可以过去吗?不使用多次提取..
获取所有事件到数组的代码:
NSDate *start = ...NSDate *完成 = ...//使用 Dictionary 删除由覆盖超过一年的事件产生的重复项NSMutableDictionary *eventsDict = [NSMutableDictionary dictionaryWithCapacity:1024];NSDate* currentStart = [NSDate dateWithTimeInterval:0 sinceDate:start];int seconds_in_year = 60*60*24*365;//以一年为单位枚举事件,因为 iOS 不支持超过 4 年的谓词!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 = [eventStore predicateForEventsWithStartDate:currentStart endDate:currentFinish calendars:nil];[eventStore 枚举EventsMatchingPredicate:谓词usingBlock:^(EKEvent *event, BOOL *stop) {如果(事件){[eventsDict setObject:event forKey:event.eventIdentifier];}}];currentStart = [NSDate dateWithTimeInterval:(seconds_in_year + 1) sinceDate:currentStart];}NSArray *events = [eventsDict allValues];
i would like to know how to fetch all events from an EventStore using EventKit in iOS.
This way i can specify all events for today:
- (NSArray *)fetchEventsForToday {
NSDate *startDate = [NSDate date];
// endDate is 1 day = 60*60*24 seconds = 86400 seconds from startDate
NSDate *endDate = [NSDate dateWithTimeIntervalSinceNow:86400];
// Create the predicate. Pass it the default calendar.
NSArray *calendarArray = [NSArray arrayWithObject:defaultCalendar];
NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:calendarArray];
// Fetch all events that match the predicate.
NSArray *events = [self.eventStore eventsMatchingPredicate:predicate];
return events;
}
The correct should use a NSPredicate, which is created with:
NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:calendarArray];
I have tried using
distantPast
distantFuture
as startDate and endDate, no good. So other A's from other Q's are not exaclty wha im looking for.
Thank you!
EDIT
I have tested and got to the conclusion that i can only fetch events in a period of 4 years maximum. Any way of getting past this? Without using multiple fetches..
Code for fetch all events into array :
NSDate *start = ...
NSDate *finish = ...
// use Dictionary for remove duplicates produced by events covered more one year segment
NSMutableDictionary *eventsDict = [NSMutableDictionary dictionaryWithCapacity:1024];
NSDate* currentStart = [NSDate dateWithTimeInterval:0 sinceDate:start];
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:finish] == NSOrderedAscending) {
NSDate* currentFinish = [NSDate dateWithTimeInterval:seconds_in_year sinceDate:currentStart];
if ([currentFinish compare:finish] == NSOrderedDescending) {
currentFinish = [NSDate dateWithTimeInterval:0 sinceDate:finish];
}
NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:currentStart endDate:currentFinish calendars:nil];
[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];
这篇关于从 EventStore EventKit iOS 中获取所有事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!