Search code examples
ioscore-datacalendarcontrolseventkit

IOS/xcode/coredata: Implement calendar control using core data


IOS newb here trying to implement an open source calendar control MADay in my project with the data stored in core data. The calendar control does not provide documentation for accessing core data.

I am guessing that I need to create an NSArray after pulling the events from core data that corresponds to the "Events" array below. Is that all? And if so, how do I do that?

Thanks for any suggestions.

Here is the datasource code from the library:

- (NSDate *)nextDayForDate:(NSDate *)date;
- (NSArray *)eventKitEventsForDate:(NSDate *)date;
- (NSArray *)eventKitEventsToMAEvents:(NSArray *)eventKitEvents;

@property (readonly) EKEventStore *eventStore;

@end

@implementation MAEventKitDataSource


- (NSArray *)dayView:(MADayView *)dayView eventsForDate:(NSDate *)date
{
    return [self eventKitEventsToMAEvents:[self eventKitEventsForDate:date]];
}

- (NSArray *)weekView:(MAWeekView *)weekView eventsForDate:(NSDate *)date
{
    return [self eventKitEventsToMAEvents:[self eventKitEventsForDate:date]];
}


- (EKEventStore *)eventStore
{
    if (!_eventStore) {
        _eventStore = [[EKEventStore alloc] init];
    }
    return _eventStore;
}


- (NSDate *)nextDayForDate:(NSDate *)date
{
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setDay:1];

    return [CURRENT_CALENDAR dateByAddingComponents:components toDate:date options:0];
}

- (NSArray *)eventKitEventsForDate:(NSDate *)startDate
{
    NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate
                                                                      endDate:[self nextDayForDate:startDate]
                                                                    calendars:nil];

    NSArray *events = [self.eventStore eventsMatchingPredicate:predicate];
    return events;
}

- (NSArray *)eventKitEventsToMAEvents:(NSArray *)eventKitEvents
{
    NSMutableArray *events = [[NSMutableArray alloc] init];
    for (EKEvent *event in eventKitEvents) {
        MAEvent *maEvent = [[MAEvent alloc] init];
        maEvent.title  = event.title;
        maEvent.start  = event.startDate;
        maEvent.end    = event.endDate;
        maEvent.allDay = event.allDay;

        maEvent.backgroundColor = [UIColor colorWithCGColor:event.calendar.CGColor];
        maEvent.textColor       = [UIColor whiteColor];

        [events addObject:maEvent];
    }
    return events;
}

Solution

  • I haven't worked myself with this framework, but as far as I can see from code and the EventKit documentation, it has an own store so it doesn't require a further core-data handling.

    https://developer.apple.com/library/prerelease/ios/documentation/EventKit/Reference/EKEventStoreClassRef/index.html#//apple_ref/occ/instm/EKEventStore/saveCalendar:commit:error:

    so: read the documentation to understand ;)