ios - 如何检测更改了哪个 EKevent

标签 ios ekevent ekeventkit ekeventstore

我遇到了问题。我需要知道我的 EventStore 中的事件何时更改,因此对于这种情况,我使用 EKEventStoreChangedNotification 但此通知返回给我的是 userInfo 中难以理解的字典 它看起来像这样:

EKEventStoreChangedObjectIDsUserInfoKey = ("x-apple-eventkit:///Event/p429" );

我不知道如何使用这些数据来访问已更改的对象。请帮助我

最佳答案

这将检测更改的事件并记录日期范围内的事件标题。虽然,我最终没有这样做,因为实际上我不知道日期范围。我需要与我正在处理的所有事件进行比较,这意味着我无论如何都需要刷新它们,因为对象 ID 可能已更改。这最终使每个事件都没有那么有用,现在我只是在发生变化时每隔几秒钟刷新一次并忽略细节。我希望 Apple 改进这些通知。

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

关于ios - 如何检测更改了哪个 EKevent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16162027/

相关文章:

ios - 相机预览上的 SwiftUI 位置叠加

ios - 如何在Parse中获取对象ID

ios - 从 NSUserDefaults 获取值时出错

ios - 如何获取 EkEvent 类型对象的 Show As 值?

ios - 日历事件添加错误时间问题

ios - 使用predicateForEventsWithStartDate:endDate:calendars方法查找EKEvents

events - 重复 requestAccessToEntityType :EKEntityTypeEvent during development 的过程

ios - Json 无法将数据转换到我的模型中

ios - 应用在后台时 NSNotificationCenter 回调

ios - 如何删除保存为 EKReminder 的提醒?