Check if an event exists in the calendar

I'm having trouble checking if an event exists in the user's calendar. I need to check this to determine whether I add it or not so that I do not duplicate calendar entries. Right now, I am duplicating the entry every time I run the code.

First, this is how I create a calendar entry:

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

This sets the event correctly. However, if I run it again, I expect the else if clause to return YES and a new record will not be created. However, it always returns NO , and I create a new calendar entry every time I execute. Here is the method:

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

When I go through this method, I notice that the NSArray is called eventOnDate nil ( EKEventStore not nil ). I do not know if this means that he simply did not find matching events or something else was happening.

What am I doing wrong that will not allow this to identify existing events in the calendar? Thanks!

+5
source share
1 answer

The problem is with the date range selected for your predicate.

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

This will look for events within the range of "0" because the start and end dates of your predicate request are identical.

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

It will display events that lie within an hour from the date provided.

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

This will create an array that spans all day events from start to finish.

+5
source

Source: https://habr.com/ru/post/1242038/


All Articles