How to fix EKErrorDomain Code = 1 "Calendar not set

I want to create a calendar entry in the iPhone calendar, I 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 the result

 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 it because

 [eventStore defaultCalendarForNewEvents]; 

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

+6
source share
4 answers

If it's on iOS 6.0 or later, you need to first request access to user calendars before EventKit passes them to you using the method - [EKEventStore requestAccessToEntityType: completion:]

See the example in the Calendar and Reminder Programming Guide

+7
source

In order not to waste time, just make sure that you use bit masks in - [EKEventStore requestAccessToEntityType: completion:]

like this

 EKEventStore *eventStore = [[EKEventStore alloc] init]; [eventStore requestAccessToEntityType:EKEntityMaskEvent completion:^(BOOL granted, NSError *error) { // ... }]; 
+2
source

Add these lines

 if(eventStore.defaultCalendarForNewEvents==nil) *eventStore = [[EKEventStore alloc] init]; 

The second line will only be executed the first time you grant access

Your code should look like this

  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; if(eventStore.defaultCalendarForNewEvents==nil) *eventStore = [[EKEventStore alloc] init]; 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]; 

it works for me

+1
source

I fixed this by making sure that the title does not match what I created, for example, I have a dance for several nights, so in one night I will do a dance and the other with a period at the beginning of .Dance .

0
source

All Articles