Fetch all events from EventStore EventKit iOS

后端 未结 3 1718
梦谈多话
梦谈多话 2020-11-29 02:44

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 *)fetc         


        
相关标签:
3条回答
  • 2020-11-29 02:56

    This is code in production

    const double secondsInAYear = (60.0*60.0*24.0)*365.0;
    NSPredicate* predicate = [eventStore predicateForEventsWithStartDate:[NSDate dateWithTimeIntervalSinceNow:-secondsInAYear] endDate:[NSDate dateWithTimeIntervalSinceNow:secondsInAYear] calendars:nil];
    

    For you, I would recommend looking back and forward ten years.

    0 讨论(0)
  • 2020-11-29 03:15

    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];
    
    0 讨论(0)
  • 2020-11-29 03:17

    This is the method I am using in my app to fetch them.

        NSDate *startDate = [NSDate distantPast];       
        NSDate *endDate = [NSDate distantFuture];
    
    0 讨论(0)
提交回复
热议问题