EventKit日历的最后一个事件

时间:2015-07-20 12:15:12

标签: ios objective-c eventkit

我正在创建一个应用程序,我将某些事件同步到iPhone上的日历。

问题是,我无法告知哪些事件被更改/删除/ ...... 所以我需要在“同步”(读取插入)新事件之前删除今天和日历最后一个事件的结束日期之间的所有事件

据我所见,一次对多个事件采取行动的唯一方法是使用enumerateEventsMatchingPredicate:usingBlock:predicateForEventsWithStartDate:endDate:calendars:

但为此,我需要一个特定的结束日期。 (因此,我日历中最后一个事件的结束日期)

我总是可以保存我插入此日历的最后一个事件的事件标识符,但我不想这样做: 如果用户卸载我的应用程序并在以后再次安装它,我将不再拥有最后一个事件标识符。 (鉴于他当然没有手动删除日历)

我可以在每次需要同步日历时删除日历,但之后我会丢失所有已通过的事件。

非常感谢任何想法或提示!

1 个答案:

答案 0 :(得分:0)

您的评论:

  

我似乎找不到任何方法来获取日历的所有事件。

实际上,您可以从日历中获取所有事件:

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];