12

iOS で EventKit を使用して EventStore からすべてのイベントを取得する方法を知りたいです。

このようにして、今日のすべてのイベントを指定できます。

- (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;
}

正しいものは、次のように作成された NSPredicate を使用する必要があります。

NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:calendarArray]; 

使ってみました

distantPast
distantFuture

startDate と endDate として、ダメです。したがって、他の Q からの他の A は、私が探している正確なものではありません。

ありがとうございました!


編集

テストした結果、最大 4 年間のイベントしか取得できないという結論に達しました。これを乗り越える方法はありますか?複数のフェッチを使用せずに..

4

3 に答える 3

13

すべてのイベントを配列にフェッチするためのコード:

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];
于 2012-01-17T01:16:01.830 に答える
0

これは、アプリでそれらを取得するために使用している方法です。

    NSDate *startDate = [NSDate distantPast];       
    NSDate *endDate = [NSDate distantFuture];
于 2011-06-16T16:07:14.353 に答える
-1

これは本番環境のコードです

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

あなたのために、10年を振り返ってみることをお勧めします。

于 2011-05-21T00:47:10.523 に答える