0
votes

I added the framework EventKit and I have this code. I can't add an event at default calendar.

#import "Calendar.h"
#import <EventKit/EventKit.h>

@implementation Calendar

-(IBAction)addCal:(id)sender
{
    EKEventStore *store = [[EKEventStore alloc] initWithAccessToEntityTypes:EKEntityMaskEvent];
    EKEvent *evento = [EKEvent eventWithEventStore:store];

    [evento setStartDate:[NSDate date]];
    [evento setTitle:@"Title"];   
    [evento setCalendar:[store defaultCalendarForNewEvents]];

    NSError *error;
    [store saveEvent:evento span:EKSpanThisEvent commit:YES error:&error];    
}

In Console I have this: "We are including and fetching events, because this application did something event-related. If this application doesn't care about events, then this is a potentially expensive call."

I'm a beginner. Thanks for your help.

1
Im starting with the eventkit, too. Can you let me know what your version is of XCode? I'm having big problems on 4.5 DP3 to even load the header file. Once that's done I will be able to look into event creation ;-)Carelinkz

1 Answers

2
votes

I successfully used this code:

    //Calendar Test:
    [self setEventStore:[[EKEventStore alloc] initWithAccessToEntityTypes:EKEntityMaskEvent]];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dc = [[NSDateComponents alloc] init];
    [dc setHour:1];
    NSDate *startDate = [NSDate date];
    NSDate *endDate = [calendar dateByAddingComponents:dc toDate:[NSDate date] options:0];
    EKEvent *anEvent = [EKEvent eventWithEventStore:eventStore];
    [anEvent setTitle:@"event1"];;
    [anEvent setCalendar:[[eventStore calendarsForEntityType:EKCalendarTypeLocal]objectAtIndex:0]];
    [anEvent setLocation:@"Somewhere"];
    [anEvent setStartDate:startDate];
    [anEvent setEndDate:endDate];
    [eventStore saveEvent:anEvent span:EKSpanThisEvent commit:YES error:nil];

It gives me a one-hour event in my first calendar, but I still do get the message. I suspect the message is a one-off debugging message, but I did not test with a release version of the program. Couldn't find anything about this in the docs, either.

Hope that helps.