上次更新时的 EKEvent

EKEvent when last updated

我目前正在构建一个 iOS 日历应用程序,它可以访问 iphone 的日历到 read/write from/to 它,这是我面临的问题是我的应用程序应该同步我的应用程序的日历和 iphone 日历,所以如果我的应用程序修改了一个事件,它应该在 iphone 的日历中进行修改,反之亦然。

class EKEvent 的一个对象似乎没有 updatedAt 属性,所以我没有办法说哪个是最新版本的给定的事件,是我的应用程序还是 iPhone 的日历。

有没有办法获取上次修改 ekevent 的时间?

提前致谢。

好的,我把我知道的都告诉你,希望对你有帮助。

你说得对,单个 EKEvent 没有最后修改日期 属性。只有 EKCalendarItem 有一个 属性 lastModifiedDate 但我不确定这对你的情况是否有用。

我发现了这个有趣的函数:

#pragma mark - Calendar Changed
- (void)calendarChanged:(NSNotification *)notification {
    EKEventStore *ekEventStore = notification.object;

    NSDate *now = [NSDate date];
    NSDateComponents *offsetComponents = [NSDateComponents new];
    [offsetComponents setDay:0];
    [offsetComponents setMonth:4];
    [offsetComponents setYear:0];
    NSDate *endDate = [[NSCalendar currentCalendar] dateByAddingComponents:offsetComponents toDate:now options:0];

    NSArray *ekEventStoreChangedObjectIDArray = [notification.userInfo objectForKey:@"EKEventStoreChangedObjectIDsUserInfoKey"];
    NSPredicate *predicate = [ekEventStore    predicateForEventsWithStartDate:now
                                                                  endDate:endDate
                                                                calendars:nil];
    // Loop through all events in range
    [ekEventStore enumerateEventsMatchingPredicate:predicate usingBlock:^(EKEvent *ekEvent, BOOL *stop) {
        // Check this event against each ekObjectID in notification
        [ekEventStoreChangedObjectIDArray enumerateObjectsUsingBlock:^(NSString *ekEventStoreChangedObjectID, NSUInteger idx, BOOL *stop) {
            NSObject *ekObjectID = [(NSManagedObject *)ekEvent objectID];
            if ([ekEventStoreChangedObjectID isEqual:ekObjectID]) {
                // Log the event we found and stop (each event should only exist once in store)
                NSLog(@"calendarChanged(): Event Changed: title:%@", ekEvent.title);
                *stop = YES;
            }
        }];
    }];
}

最初发布 in this answer 但似乎使用了私有 API。

最后,请注意在 EKEvent 的 属性 eventIdentifier 中:

If the calendar of an event changes, its identifier most likely changes as well.

也许此信息会有所帮助,请在 the Apple Documentation 中查看更多信息。