xcode - EKEvent 上次更新时间

标签 xcode calendar ekevent ekeventkit

我目前正在构建一个iOS日历应用程序,可以访问iPhone的日历以对其进行读取/写入,我面临的问题是我的应用程序应该同步我的应用程序的日历和iPhone日历,所以如果我的应用程序修改了某个事件,则该事件应该在 iPhone 的日历中进行修改,反之亦然。

EKEvent 类的对象似乎没有 updateAt 属性,因此我无法确定哪个是给定事件的最新版本,是我的应用程序的还是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但似乎使用了私有(private) API。

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

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

也许此信息会有所帮助,请参阅the Apple Documentation了解更多信息.

关于xcode - EKEvent 上次更新时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32718942/

相关文章:

ios - 已打开应用程序中的 URL Scheme

iOS - 设置 bundle 中文本字段中小数点键盘的键盘类型

facebook - 在 Facebook 中导出事件 URL

ios - 当我们的应用程序在 iPad 中打开时,我们可以禁用日历事件通知弹出窗口吗?

ios - EKEvent 的 eventIdentifier 和 calendarItemExternalIdentifier 的区别

ios - 保存 80 个事件后收到内存警告

ios - EKEventKit - 如何在批量提交后检索 eventIdentifier?

ios - 如何正确设置水平 ImageView 的自动布局约束?

python - Outlook 日历 API - Python

ios - 需要 NSBluetoothAlwaysUsageDescription,但不使用蓝牙