我目前正在构建一个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/