【问题标题】:EKEvent Get Current Event iOS7EKEvent 获取当前事件 iOS7
【发布时间】:2014-01-16 21:07:00
【问题描述】:

所以我需要在日历中获取当前事件。 I.E - 一个已经开始但尚未结束的事件。我写了一些代码,但它不起作用。 通过调试,我注意到我的 oneDayAgo 变量为零,我不明白为什么。 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];

                      }];

}

【问题讨论】:

    标签: ios ios7 ekevent ekeventkit ekeventstore


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • 谢谢!没有注意到那个错字。多么愚蠢的错误......很好看:*
    • @MichalShatz Pleasure :-) 这是一个“及时”的问题——就在我喝了一杯浓缩咖啡之后。
    【解决方案2】:

    这是对我有用的更正代码。我还需要修改一些其他的东西:

    -(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]] == NSOrderedAscending) &&
                                      ([[NSDate date] compare:event.endDate] == NSOrderedAscending)) {
                                      [currentEvens addObject:event];
                                  }
    
                              }];
    
                              NSString *currentEventsString = [[NSString alloc]init];
                              for (EKEvent *event in currentEvens) {
                                  currentEventsString = [currentEventsString stringByAppendingString:event.title];
                              }
    
                              dispatch_async(dispatch_get_main_queue(), ^{
                                  self.lblEvent.text = currentEventsString;
                              });
    
                          }];
    

    }

    【讨论】:

      猜你喜欢
      • 2014-01-03
      • 2016-04-15
      • 1970-01-01
      • 2012-07-30
      • 1970-01-01
      • 1970-01-01
      • 2018-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多