Search code examples
ioscalendareventkit

Check EKCalendar


Good day! I use function "Save event to calendar" via UIActivityItems. In that function I create new calendar and add event to this calendar:

EKEventStore* eventStore = [[EKEventStore alloc] init];

// Get the calendar source
EKSource* localSource;
for (EKSource* source in eventStore.sources) {
    if (source.sourceType == EKSourceTypeLocal)
    {
        localSource = source;
        break;
    }
}

if (!localSource)
    return;

EKCalendar *newCalendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:eventStore];
calendar.source = localSource;
calendar.title = @"New Calendar";

NSError *errorCalendar;
[eventStore saveCalendar:newCalendar commit:YES error:&errorCalendar]; 

EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
event.title     = @"Title";

event.startDate = startDate;
event.endDate   = endDate;

[event setCalendar:newCalendar];
// and etc.

And its working. But every next time it create new calendar with name "New Calendar" again. How can I check if calendar with that name already exist? And how can I change type of calendar? On birthday or etc.


Solution

  • First, you need to use a single instance of the EventStore for the life of your app, according to Apple.

    Therefore I suggest making the eventStore a property of your view controller:

    @property (nonatomic, retain) EKEventStore *eventStore;

    and in your viewDidLoad:

    self.eventStore = [[EKEventStore alloc] init];

    Now you can check the same eventStore instance that you are reading and writing to before you do anything:

    -(BOOL)checkForCalendar {
        //get an array of the user's calendar using your instance of the eventStore
        NSArray *calendarArray = [self.eventStore calendarsForEntityType:EKEntityTypeEvent];
    
        // The name of the calendar to check for. You can also save the calendarIdentifier and check for that if you want
        NSString *calNameToCheckFor = @"New Calendar";
    
        EKCalendar *cal;
    
        for (int x = 0; x < [calendarArray count]; x++) {
    
            cal = [calendarArray objectAtIndex:x];
            NSString *calTitle = [cal title];
    
            // if the calendar is found, return YES
            if (([calTitle isEqualToString:calNameToCheckFor]) {
    
                return YES;
            }
        }
    
        // Calendar name was not found, return NO;
        return NO;
    }
    
    -(void)saveNewEvent {
    
        // If the calendar does not already exist, create it before you save the event.
        if ([self checkForCalendar] == NO) { 
    
            // Get the calendar source
            EKSource* localSource;
            for (EKSource* source in eventStore.sources) {
                if (source.sourceType == EKSourceTypeLocal)
                {
                    localSource = source;
                    break;
                }
            }
    
            if (!localSource)
                return;
    
            EKCalendar *newCalendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:eventStore];
            calendar.source = localSource;
            calendar.title = @"New Calendar";
    
            NSError *errorCalendar;
            [eventStore saveCalendar:newCalendar commit:YES error:&errorCalendar];
    
        } 
            EKEvent *event  = [EKEvent eventWithEventStore:self.eventStore];
            event.title     = @"Title";
            event.startDate = startDate;
            event.endDate   = endDate;
            [event setCalendar:newCalendar];
            // and etc.
    }