5
votes

I am developing a calendar app using eventkit framework in ios 6. I am trying to get the permission using the [self.store respondsToSelector:@selector(requestAccessToEntityType:completion:)] method and after getting permission to access the calendars, I am trying to create the new calendar with identifier using EKSourceTypeLocal source and add events to the newly created calendar. I am facing the problem here that when I try to run the app in iPhone 4s it shows the error that "calendar has no source" and it doesn't save my calendar and hence no events gets added to the calendar. I don't know what am I doing wrong here. Please guide me to solve this issue.

I am posting my code below

   - (void)viewDidLoad
{
    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    NSLog(@"in view did load method");


    // For iOS 6.0 and later
    self.store = [[EKEventStore alloc] init];
    if([self.store respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {

    [self.store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        // handle access here
        dispatch_async(dispatch_get_main_queue(), ^{

            if (granted) {

                NSLog(@"permission granted");

                EKCalendar *myCalendar;

                EKSource *myLocalSource = nil;
                for (EKSource *calendarSource in self.store.sources) {
                    if (calendarSource.sourceType == EKSourceTypeLocal) {
                        myLocalSource = calendarSource;
                        break;
                    }
                }

                if (!myLocalSource)
                    return;

                NSString *mycalIndentifier = [[NSUserDefaults standardUserDefaults] valueForKey:@"my_calendar_identifier"];

                if (mycalIndentifier == NULL) {

                    // Create a new calendar of type Local... save and commit
                    myCalendar  = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:self.store];
                    myCalendar.title = @"New_Calendar";
                    myCalendar.source = myLocalSource;


                    NSString *calendarIdentifier = myCalendar.calendarIdentifier;

                    NSError *error = nil;
                    [_store saveCalendar:myCalendar commit:YES error:&error];

                    if (!error) {
                        NSLog(@"created, saved, and commited my calendar with id %@", myCalendar.calendarIdentifier);

                        [[NSUserDefaults standardUserDefaults] setObject:calendarIdentifier forKey:@"my_calendar_identifier"];

                    } else {
                        NSLog(@"an error occured when creating the calendar = %@", error.description);
                        error = nil;
                    }

                    //create an event

                    // Create a new event... save and commit
                    EKEvent *myEvent = [EKEvent eventWithEventStore:_store];
                    myEvent.allDay = NO;
                    myEvent.startDate = [NSDate date];
                    myEvent.endDate = [NSDate date];
                    myEvent.title = @"Birthday";
                    myEvent.calendar = myCalendar;
                    [_store saveEvent:myEvent span:EKSpanThisEvent commit:YES error:&error];

                    if (!error) {
                        NSLog(@"the event saved and committed correctly with identifier %@", myEvent.eventIdentifier);
                    } else {
                        NSLog(@"there was an error saving and committing the event");
                        error = nil;
                    }

                    EKEvent *savedEvent = [_store eventWithIdentifier:myEvent.eventIdentifier];
                    NSLog(@"saved event description: %@",savedEvent);

                }
                else{

                    myCalendar = [_store calendarWithIdentifier:mycalIndentifier];
                }

            }
            else if(!granted){

                NSLog(@"Permission not granted");

            }
            else{

                NSLog(@"error = %@", error.localizedDescription);
            }

        });

    }];
  }


}


this is the error I am getting :

Predicate call to calendar daemon failed: Error Domain=EKCADErrorDomain Code=1013 "The operation couldn’t be completed. (EKCADErrorDomain error 1013.)"

not saved = Error Domain=EKErrorDomain Code=14 "Calendar has no source" UserInfo=0x1d5f6950 {NSLocalizedDescription=Calendar has no source}

UPDATE : I solved the problem using this link try it

1
Please close as duplicate of stackoverflow.com/questions/12454324/…jrc

1 Answers

3
votes

Problem seems to be that the local calendar is hidden when iCloud is enabled so you need to handle both cases (iCloud enabled and not).

See this answer for a working solution: https://stackoverflow.com/a/15980556/72176