我从Web服务收到一些事件。我使用Eventkit以编程方式添加了这些事件。我有一个Add
按钮,该按钮调用代码以添加接收到的事件。但是,重复的添加操作将复制事件,而不是合并事件(如果事件已经存在)。
如何避免事件重复?
最佳答案
您必须先检查现有事件,然后再添加它。以下代码显示了如何执行此操作。我已经单独检查标题和开始日期来确定事件,您可以根据需要使用更多参数。
EKEventStore *eventStore = [[EKEventStore alloc] init];
NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:[startDate dateByAddingTimeInterval:-60]
endDate:[endDate dateByAddingTimeInterval:60] calendars:nil];
NSArray *events = [eventStore eventsMatchingPredicate:predicate];
bool found = NO;
for ( EKEvent *evt in events )
{
if ( [evt.title isEqualToString:title] && [evt.startDate isEqualToDate:startDate])
{
found = YES;
break;
}
}
if(!found)
{
//Add your event here.
}
关于ios - 如何使用EventKit处理重复事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22370412/