EKEvent获取当前事件iOS7

米哈尔·沙茨(Michal Shatz)

因此,我需要在日历中获取当前事件。IE-开始但尚未结束的事件。我已经写了一些代码,但是没有用。通过调试,我注意到我的oneDayAgo变量为nil,我不明白为什么。oneWeekFromNow变量很好。

这是我写的方法:

-(void)getCurrentEvent{
// Get appropriate calendar
[self.store requestAccessToEntityType:EKEntityTypeEvent
                      completion:^(BOOL granted, NSError *error) {
                          NSCalendar *calendar = [NSCalendar currentCalendar];
                          NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];
                          oneDayAgoComponents.day -=1;
                          NSDate *oneDayAgo = [calendar dateByAddingComponents:oneDayAgoComponents
                                                                        toDate:[NSDate date]
                                                                       options:0];
                          NSDateComponents *oneWeekFromNowComponents = [[NSDateComponents alloc] init];
                          oneWeekFromNowComponents.week = 1;
                          NSDate *oneWeekFromNow = [calendar dateByAddingComponents:oneWeekFromNowComponents
                                                                             toDate:[NSDate date]
                                                                            options:0];
                          NSPredicate *predicate = [self.store predicateForEventsWithStartDate:oneDayAgo
                                                                                       endDate:oneWeekFromNow
                                                                                     calendars:nil];

                          NSMutableArray *currentEvens = [[NSMutableArray alloc]init];
                          // Fetch all events that match the predicate
                          [self.store enumerateEventsMatchingPredicate:predicate usingBlock:^(EKEvent *event, BOOL *stop) {
                              if (([event.startDate compare:[NSDate date]] == NSOrderedDescending) &&
                                  ([[NSDate date] compare:event.endDate] == NSOrderedDescending)) {
                                  [currentEvens addObject:event];
                              }

                          }];


                          self.lblEvent.text = [NSString stringWithFormat:@"%@", currentEvens];
                          [self.view reloadInputViews];

                      }];

}

邪恶的

尝试以下方法:

NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];
oneDayAgoComponents.day = -1;

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章