Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix EKErrorDomain Code=1 "No calendar has been set

I want to create a calendar entry to the iPhone calendar, I have tried the following code

        EKEventStore *eventStore = [[EKEventStore alloc] init];
        EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
        event.title     = self.selectedPost.postTitle;
        event.notes     = self.selectedPost.postContent;
        event.startDate =  self.selectedPost.startDate;
        event.endDate   =  self.selectedPost.endDate;

        EKCalendar *targetCalendar = nil;
        targetCalendar = [eventStore defaultCalendarForNewEvents];
        NSLog(@"%@",targetCalendar);
        [event setCalendar:targetCalendar];
        NSError *err;
        [eventStore saveEvent:event span:EKSpanThisEvent error:&err];
        UIAlertView *alert = nil;
        NSLog(@"err %@",err);
        if (err) {
            alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[err localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
        }
        else{
            alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Added to calender" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
        }

        [alert show];

but result is

2013-01-15 22:31:34.682 Project[40863:907] defaultCalendarForNewEvents failed: Error Domain=EKCADErrorDomain Code=1013 "The operation couldn’t be completed. (EKCADErrorDomain error 1013.)"
2013-01-15 22:31:34.683 Project[40863:907] (null)
2013-01-15 22:31:34.690 Project[40863:907] err Error Domain=EKErrorDomain Code=1 "No calendar has been set." UserInfo=0x1d535ba0 {NSLocalizedDescription=No calendar has been set.}

I know this is because of

[eventStore defaultCalendarForNewEvents];

returns null. I have tried [eventStore calendarWithIdentifier:event.calendarItemIdentifier]; and some other code but same result how to fix this Any idea

like image 953
Johnykutty Avatar asked Jan 15 '13 17:01

Johnykutty


2 Answers

If this is on iOS 6.0 or later, you'll have to first request access to the user's calendars before EventKit will hand them to you by using the method -[EKEventStore requestAccessToEntityType:completion:]

Check out the example given in the Calendar and Reminders Programming Guide

like image 199
Jeff Smith Avatar answered Oct 16 '22 15:10

Jeff Smith


For the sake of not-wasting-your-time just make sure, that you are using the bit masks in -[EKEventStore requestAccessToEntityType:completion:]

like this

EKEventStore *eventStore = [[EKEventStore alloc] init];
[eventStore requestAccessToEntityType:EKEntityMaskEvent completion:^(BOOL granted, NSError *error) {
    // ...
}];
like image 21
leviathan Avatar answered Oct 16 '22 15:10

leviathan