检查日历上是否存在事件

时间:2016-02-01 20:52:44

标签: ios objective-c ekeventstore

我无法验证用户日历中是否已存在某个事件。我需要检查一下以确定是否应该添加它,以便我不创建重复的日历条目。现在,我每次运行代码时都会创建一个重复的条目。

首先,这是我创建日历条目的方式:

+ (NSString *) addEventToCalenderWithDate : (NSDate *) eventDate
                               eventTitle : (NSString *) eventTitle
                            eventLocation : (NSString *) eventLocation
                              allDayEvent : (BOOL) isAllDay
{
    EKEventStore *store = [[EKEventStore alloc] init];

[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {

    if (!granted) {

        returnValue = @"calendar error";

    }

    else if ([self eventExists:dateAndTime eventTitle:eventTitle allDayEvent:isAllDay]) {

        returnValue = @"duplicate";

    }

    else {

        EKEvent *event = [EKEvent eventWithEventStore:store];
        event.title = eventTitle;

        event.startDate = dateAndTime;

        if (eventTimeString == (id)[NSNull null] || eventTimeString.length == 0 || isAllDay) {
            event.allDay = YES;
            event.endDate = dateAndTime;
        } else {
            event.endDate = [event.startDate dateByAddingTimeInterval:60*60];  //set 1 hour meeting
        }

        event.location = eventLocation;
        [event setCalendar:[store defaultCalendarForNewEvents]];
        NSError *err = nil;
        [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];

        returnValue = @"success";
    }
}];

return returnValue;


}

这会正确设置事件。但是,如果我再次运行它,我希望else if子句将返回YES并且不会创建新条目。但是,它始终返回NO,并且每次执行时都会创建一个新的日历条目。这是方法:

+ (BOOL) eventExists : (NSDate *) date
          eventTitle : (NSString *) eventTitle
         allDayEvent : (BOOL) isAllDay
{
    EKEventStore *store = [[EKEventStore alloc] init];

    NSPredicate *predicateForEventOnDate = [[NSPredicate alloc] init];

    if (isAllDay)
        predicateForEventOnDate = [store predicateForEventsWithStartDate:date endDate:date calendars:nil]; // nil will search through all calendars
    else
        predicateForEventOnDate = [store predicateForEventsWithStartDate:date endDate:[date dateByAddingTimeInterval:60*60] calendars:nil]; // nil will search through all calendars

    NSArray *eventOnDate = [store eventsMatchingPredicate:predicateForEventOnDate];
    NSLog(@"eventOnDate: %@", eventOnDate);

    BOOL eventExists = NO;

    for (EKEvent *eventToCheck in eventOnDate) {
        if ([eventToCheck.title isEqualToString:eventTitle]) {
            eventExists = YES;
        }
    }

    return eventExists;
}

当我逐步完成此方法时,我注意到名为NSArray的{​​{1}}为eventOnDatenil不是EKEventStore)。我不知道这是否意味着它根本没有找到任何匹配的事件,或者是否还有其他事情发生。

我做错了什么,不能让它识别日历上的现有事件?谢谢!

1 个答案:

答案 0 :(得分:7)

问题似乎与您为谓词选择的日期范围有关。

predicateForEventOnDate = [store predicateForEventsWithStartDate:date endDate:date calendars:nil];

这将查找“0”秒范围内的事件,因为谓词查询的开始日期和结束日期是相同的。

predicateForEventOnDate = [store predicateForEventsWithStartDate:date endDate:[date dateByAddingTimeInterval:60*60] calendars:nil];

这只会查找距离提供日期一小时内的事件。

NSCalendar *const calendar = NSCalendar.currentCalendar;
NSCalendarUnit const preservedComponents = (NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay);

//strip away hours, minutes and seconds to find date - at start of day
NSDateComponents *startComponents = [calendar components:preservedComponents fromDate:self.date];
//set finished date to 1 full day later
NSDateComponents *offset = [[NSDateComponents alloc] init];
[offset setDay:1];

NSDate *start = [calendar dateFromComponents:startComponents];
NSDate *finish = [calendar dateByAddingComponents:offset toDate:self.date options:0];

NSPredicate *predicateForEventOnDate = [[NSPredicate alloc] init];

if (isAllDay)
    predicateForEventOnDate = [store predicateForEventsWithStartDate:start endDate:finish calendars:nil];

NSArray *eventOnDate = [store eventsMatchingPredicate:predicateForEventOnDate];

这将生成一个数组,涵盖从开始到结束的整天的事件。