6

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

Johnykutty
  • 12,091
  • 13
  • 59
  • 100
  • How did you solve this issue? I am already making the request to access user's calendar using the method given in accepted answer. I am getting defaultCalendarForNewEvents as nil, even after I instantiate it again inside the completion block. Any idea? – Skywalker Oct 10 '16 at 09:07
  • @MeharoofNajeeb which entity type (`EKEntityType`) you are passing? – Johnykutty Oct 11 '16 at 06:15
  • @MeharoofNajeeb if you are passing `EKEntityTypeReminder` the `defaultCalendarForNewEvents ` will be nil. then you should use `-[EKEventStore defaultCalendarForNewReminders]` – Johnykutty Oct 11 '16 at 06:28
  • I was passing EKEntityTypeEvent. It got fixed. I checked the calendar app and there was no calendar to select from (iOS bug I think). When I restarted the phone, the list got populated and the error fixed itself. Thanks for your time.. – Skywalker Oct 12 '16 at 05:08

4 Answers4

8

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

Jeff Smith
  • 248
  • 1
  • 7
2

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) {
    // ...
}];
leviathan
  • 11,080
  • 5
  • 42
  • 40
0

I fixed it by making sure that the title wasn't the same as the one that i was creating, for example I have dance on several nights so for one night i would do Dance and the other one with a period at the beginning .Dance.

ssedano
  • 8,322
  • 9
  • 60
  • 98
Celia
  • 1
0

Add those lines

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

Second line will execute only first time when you grant access

Your code should look like this below

    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];

this works for me

Almin
  • 29
  • 2
  • i write this line: if(self.appDelegate.eventManager.eventStore.defaultCalendarForNewEvents==nil) *self.appDelegate.eventManager.eventStore = [[EKEventStore alloc] init]; Then show error Assigning to Eveventstore from incompatible type Ekeventatore – Gami Nilesh Oct 31 '15 at 10:25
  • What is that selectedPost,that is an error in my code,please explain it. – Preetha Jun 13 '16 at 05:31