Detecting if EKStore allows creating calendars

I am working on an app that needs to post data to a user's calDAV calendar (iCloud or other). To do this, I need to determine if this source allows the creation of calendars and reminders.

EKSource doesn't offer much in terms of detecting anything other than giving you the type of source (local, calDAV, Exchange ...)

The only way I decided to find out, if possible, is to try and write a new Calendar and look at any error messages like:

 -(BOOL)ekSourceWritesToEvents:(EKSource *)ekSource {
    BOOL writesToEvents = NO;
    //Try to write a calendar to it if it fails, return NO
    //If you succeed, return YES, since this is uncommitted, no damage done
    NSError *error = nil;
    EKCalendar *testCalendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:self.eventStore];
    testCalendar.title = @"TestCalendar";
    testCalendar.source =  ekSource;

    BOOL result = [self.eventStore saveCalendar:testCalendar commit:NO error:&error];
    if (result) {
        NSLog(@"This Source can create calendars: %@",ekSource.title);

        //Now check if one can create an event
        EKEvent *newEvent = [EKEvent eventWithEventStore:self.eventStore];
        newEvent.title = @"TestEvent";
        newEvent.startDate = [NSDate date];
        newEvent.endDate = [NSDate date];
        newEvent.calendar = testCalendar;

        NSError *eventCreateError = nil;
        [self.eventStore saveEvent:newEvent span:EKSpanThisEvent commit:NO error:&eventCreateError];

        if (eventCreateError) {
            NSLog(@"Cannot Create EKEvent in test Calendar %@",eventCreateError.localizedDescription);
            writesToEvents = NO;
        } else {
            writesToEvents = YES;
            //Delete it even if uncommitted or it seems to get commited at some point
            NSError *eventDeleteError = nil;
            [self.eventStore removeEvent:newEvent span:EKSpanThisEvent commit:YES error:&eventDeleteError];
            if (eventDeleteError) {
                NSLog(@"Error removing event: %@",eventDeleteError.localizedDescription);
            }

        }
        NSError *calendarDeleteError = nil;
        [self.eventStore removeCalendar:testCalendar commit:YES error:&calendarDeleteError];
        if (calendarDeleteError) {
            NSLog(@"Error removing test calendar: %@",calendarDeleteError.localizedDescription);
        }

    } else {
        NSLog(@"Cannot Save Calendar: %@.", error);
    }
    return writesToEvents;
}

      

I find this an awkward way of doing things ... wouldn't there be a way to detect this correctly? I would prefer that the user knows what to expect before choosing the destination source, so I want to do this before trying.

Thank!

UPDATE: It seems checking is not enough to create a Calendar. You should also check that the calendar you created allows writing ... I'm investigating. Why is it possible to create a calendar and not allow it to be written outside of me ...

Update 2: It seems that even if we are not committing transactions, the test calendar is being generated somehow. So I added some code to remove the test event and calendar.

UPDATE 3: More issues ... checking the calendar creation is not all ... some services (Google) allow calendar events to be created, but not calendars. You must create a Calendar using the Google site or their API. Also, it seems like a bug with Exchange servers when trying to create an EKEvent, it actually gives you a message that the calendar cannot create reminders. Weird.

+3


source to share


2 answers


If you are using an Event Viewer Controller, you can simply use it to get the calendar.



-(EKCalendar *)eventEditViewControllerDefaultCalendarForNewEvents:(EKEventEditViewController *)controller {

    EKCalendar *defaultCalendar = [[eventStore defaultCalendarForNewEvents] init];

    return defaultCalendar;
}

      

0


source


I came across this while trying to solve the same problem. I was thinking about trying to test the creation of the calendar, so I'm with you on this side. Instead of then testing event creation, why not just check the new calendar to see if it allows events to be changed using the allowContentModifications property?



0


source







All Articles