Check if Event exists on Calendar
<p>I'm having trouble verifying if an event already exists on the user's calendar. I need to check this to determine if I should add it or not, so that I don't create duplicate calendar entries. Right now, I create a duplicate entry every time I run the code.</p>

<p>First, here is how I am creating the calendar entry:</p>

<pre><code>+ (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:&amp;err];

        returnValue = @"success";
    }
}];

return returnValue;


}
</code></pre>

<p>This sets the event correctly. However, if I run it again, I expect that the else if clause will return <code>YES</code> and no new entry will be created. However, it always returns <code>NO</code> and I create a new calendar entry with each execution. Here is that method:</p>

<pre><code>+ (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;
}
</code></pre>

<p>As I step through this method, I notice that the <code>NSArray</code> called <code>eventOnDate</code> is <code>nil</code> (the <code>EKEventStore</code> is not <code>nil</code>). I don't know if this means that it simply did not find any matching events or if something else is going on.</p>

<p>What am I doing wrong that won't allow this to identify existing events on the calendar? Thank you!</p>