这是我检索事件的代码
NSDate *now = [NSDate date];
NSDate *startDate = [self dateByAddingYears:-1 toDate:now];
NSDate *endDate = [self dateByAddingYears:1 toDate:now];
NSArray *events = [[ECEventStoreProxy sharedInstance] eventsFrom:[startDate beginningOfDay] to:[endDate endOfDay]];
self.sections = [NSMutableDictionary dictionary];
for (EKEvent *event in events)
{
// Reduce event start date to date components (year, month, day)
NSDate *dateRepresentingThisDay = [self dateAtBeginningOfDayForDate:event.startDate];
// If we don't yet have an array to hold the events for this day, create one
NSMutableArray *eventsOnThisDay = [self.sections objectForKey:dateRepresentingThisDay];
if (eventsOnThisDay == nil) {
if (event.calendar.allowsContentModifications) {
eventsOnThisDay = [NSMutableArray array];
// Use the reduced date as dictionary key to later retrieve the event list this day
[self.sections setObject:eventsOnThisDay forKey:dateRepresentingThisDay];
}
}
// Add the event to the list for this day
[eventsOnThisDay addObject:event];
}
这是记录的错误
MyApp [5173:1001]从文件系统加载时区数据时出错。这是一个严重的问题。日历数据不保证准确。
如果开始日期更改为当前日期,则表现完美...但我想显示过去1年的活动列表......
编辑1:
- (NSDate *)dateByAddingYears:(NSInteger)numberOfYears toDate:(NSDate *)inputDate
{
// Use the user's current calendar
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setYear:numberOfYears];
NSDate *newDate = [calendar dateByAddingComponents:dateComps toDate:inputDate options:0];
return newDate;
}